Design patterns in Magento
Aug 05, 2015
Design patterns in Magento
MVC - Model View Controller
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
MVC
MVC in MVC
The view layer in Magento itself implements the MVC pattern.
M - blocksV - templates.phtmlC – layouts (.xml files)
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
Modularity
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
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
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
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
Factory method inMagento
1
3
Factory method in Magento
by the way - Singleton pattern in Magento
1
4
Factory method in Magento
1
5
What's under the hood?
we can also create a
class object by giving
its name
1
6
What's under the hood?
.....
divide the string of characters
first, look for overwritten classes
1
7
What's under the hood?
Magento adds a ‘mage' prefix as a
namespace
Registry
Registry
It allows you to place
objects and data in the global
pool, allowing access to them
from anywhere in the code
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
Event/Observer
2
2
Event/Observer
The extension code for theMagento core is locatedentirely in the Observerclass
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
2
4
Event/Observer
calls the appropriate method
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
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
Front controller
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
2
9
Magento Front controller
our candidate
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?
3
1
What's under the hood?
What have we here?
Get front controller
Call
Call
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
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
Prototype
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
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.
3
7
It all becomes clear
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
Prototype
3
9
Prototyp w Magento
4
0
the facory method is called and
we transfer a product object as a
parameter
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
There are more design patterns in Magento, I strongly encourage you to explore them
Thank you for attention