Design patterns in Magento
-
Upload
divante -
Category
Technology
-
view
339 -
download
0
Transcript of Design patterns in Magento
![Page 1: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/1.jpg)
Design patterns in Magento
![Page 2: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/2.jpg)
MVC - Model View Controller
![Page 3: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/3.jpg)
3
• Division into 3 main parts of the application:
• Model - represents the application logic
• View - describes how to display data within the user interface
• Controller - accepts input from the user, updates the model and refreshes the view
MVC
![Page 4: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/4.jpg)
MVC
![Page 5: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/5.jpg)
MVC in MVC
The view layer in Magento itself implements the MVC pattern.
M - blocksV - templates.phtmlC – layouts (.xml files)
![Page 6: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/6.jpg)
MVC in MVCIn xml files we define the blocks, which are part of the view for a given controller action and assign the appropriate template to the main block
present the data obtained from the block
load and render the layoutdownload data from the model layer, process it and send to the template
![Page 7: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/7.jpg)
Modularity
![Page 8: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/8.jpg)
8
• Patterns orders grouping of individual functions in the project into modules
• Modules should not require other modules for proper operation
• Patterns define the directory structure within the parent module directory
Modularity
![Page 9: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/9.jpg)
9
• Magento autoloader haspre-established search priorities
• Subdirectory structure is not only a matter of convention.
• Magento class name reflects its path
• The individual components of the Module must be explicitly declared the module configuration file
Modularity in Magento
code pools
Subdirectory structure in the module’s main directory
![Page 10: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/10.jpg)
1
0
A short digression - Autoloader
Code pools are added in a pre-
defined order
set up php of our path of attaching files
registering the autoloader
![Page 11: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/11.jpg)
1
1
A short digression - Autoloader
The autoloader is called upon class instantiation, unless there is already an object of a
given class in the memory - the file is already attached
note that this is not a built-in
implementation of __autoload () method
Attach the desired file
![Page 12: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/12.jpg)
Factory method inMagento
![Page 13: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/13.jpg)
1
3
Factory method in Magento
by the way - Singleton pattern in Magento
![Page 14: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/14.jpg)
1
4
Factory method in Magento
![Page 15: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/15.jpg)
1
5
What's under the hood?
we can also create a
class object by giving
its name
![Page 16: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/16.jpg)
1
6
What's under the hood?
.....
divide the string of characters
first, look for overwritten classes
![Page 17: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/17.jpg)
1
7
What's under the hood?
Magento adds a ‘mage' prefix as a
namespace
![Page 18: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/18.jpg)
Registry
![Page 19: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/19.jpg)
Registry
It allows you to place
objects and data in the global
pool, allowing access to them
from anywhere in the code
![Page 20: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/20.jpg)
2
0
Registry
If you call a method with graceful parameter set to true,
you can avoid the occurence of an exception informing
you that there is a value under the given key
if at a given index there is an object that has a
destructor, use it
![Page 21: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/21.jpg)
Event/Observer
![Page 22: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/22.jpg)
2
2
Event/Observer
The extension code for theMagento core is locatedentirely in the Observerclass
![Page 23: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/23.jpg)
2
3
How can you add your own observer?events node
name of the event
module name/class name
name of the calling method at
the event
unique observer ID
![Page 24: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/24.jpg)
2
4
Event/Observer
calls the appropriate method
![Page 25: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/25.jpg)
2
5
Event/Observer
Object $area represents certain parts of
the application (frontend, global, admin,
adminhtml)
download the configuration of events for
each part of the application
iterate through all observers for a given
part of the application
Assign a taable of observers to a table of
events under the key of an event name
that they are observing
![Page 26: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/26.jpg)
2
6
Event/Observer
for each of the observers we try to call its
event operating procedures
assign the observer the
arguments transferred in the
event call
the function calling the observer’s method
call the method by transferring the observer’s
object with the arguments submitted earlier
![Page 27: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/27.jpg)
Front controller
![Page 28: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/28.jpg)
Front controller
It is the entrance gate for all requests directed at the running applications..It consists of two parts:• web handler - parses the
URL and determines whichcontroller is to be created
• command dispatch -creates a controller and forwards the request
![Page 29: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/29.jpg)
2
9
Magento Front controller
our candidate
![Page 30: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/30.jpg)
3
0
The front controller is designed to accept a request and decide what to do with it. Does it all really happen in the index.php file?
Is that so?
![Page 31: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/31.jpg)
3
1
What's under the hood?
What have we here?
Get front controller
Call
Call
![Page 32: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/32.jpg)
3
2
What's under the hood?
Iterate through router info table
containing the name of the router class
and its place of operation (frontend,
admin)
add the created router to the variable
_routers by the method addRouter (). We
will use them in the near future
also a default router is created and added
![Page 33: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/33.jpg)
3
3
What's under the hood?
Use the completed _routers table
We adapt the request to the router. Match is a method of the abstract class Mage_Core_Controller_Varien_Router_Abstract, so each router defines its own body of this method
In the match() method, check if the request matches
the address of the router and, if so, transfer the request
to the target module controller
Send an answer to the http client
![Page 34: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/34.jpg)
Prototype
![Page 35: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/35.jpg)
Prototype
The Prototype pattern is a kind of extension of the abstract factory pattern. Since it hasn’t been discussed, it will be brieflydescribed below
![Page 36: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/36.jpg)
A short digression – abstract facory
It defines a set of factory classes that inherit
from the abstract parent class (factory), which
specialize in creating groups of objects from
one family.
![Page 37: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/37.jpg)
3
7
It all becomes clear
![Page 38: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/38.jpg)
Prototype
It is a variation of abstract factory patternIt helps to limit the number of factories by creating a generic factory which is sent the types of objects to be produced
![Page 39: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/39.jpg)
Prototype
3
9
![Page 40: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/40.jpg)
Prototyp w Magento
4
0
the facory method is called and
we transfer a product object as a
parameter
![Page 41: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/41.jpg)
Prototyp w Magento
4
1
download type id transferred in the
product parameter
from the type table and corresponding names of
model classes we download the one correct for
our product
concretize object type of a product and set up the
product to operate on
![Page 42: Design patterns in Magento](https://reader034.fdocuments.in/reader034/viewer/2022042716/55c13399bb61eb7e3e8b4638/html5/thumbnails/42.jpg)
There are more design patterns in Magento, I strongly encourage you to explore them