Magento For PHP MVC
Magento For PHP MVC
Who am I?
Alan Storm http://alanstorm.com Got involved in the Internet/Web 1995 Work in the Agency/Startup Space 10 years PHP experience The thing you can never say ...
Sunday, October 10, 2010
Magento is similar to PHP Frameworks Magento and Kohana, side by side But rst
Sunday, October 10, 2010
HTML Page
HTML Page
Very radical shift No programming needed, just markup No proprietary le format, just ASCII text
Perl/CGI
Perl was once the new hotness Excelled as string processing, and web
applications are strings over HTTP
Perl/CGI
CGI was slow, and FastCGI too new Perl didnt scale way before ruby didnt CPANs good, but no standard installation
PHP 3/PHP 4
Build important features directly into
language, distributed by default
PHP Page
Shopping Cart Applications CMS systems (Drupal, Mambo, Joomla) Programming Frameworks, many inspired
by Ruby on Rails (CakePHP, Code Igniter, Symfony, etc.)
MVC Routing
Determines the main code entry point for
any particular URL request
Replaces PHPs one-page/one-url system All HTTP requests routed through a single
index.php le
Kohana Routing
Routing handled by Router_Core::setup URI: /controllerName/actionName URI: /welcome/index Welcome_Controller::index(); File: /application/controllers/welcome.php
Sunday, October 10, 2010
Magento Routing
URI: /frontName/controllerName/actionName URI: /catalogsearch/result/index No Standard Router Class!
Magento Routing
Magento Searches Global Cong for
routers
/app/code/core/Mage/Core/etc/cong.xml
Sunday, October 10, 2010
web/routers
Mage_Core_Controller_Varien_Router_Admin Mage_Core_Controller_Varien_Router_Standard
Magento Routing
The match method is called on each router The match method is responsible for Creating a Controller class name Instantiating the Controller Creating an Action method Calling the Action method
Sunday, October 10, 2010
Magento Routing
URI: /frontName/controllerName/actionName URI: /catalogsearch/result/index Default routers will any module with a congured Identify <frontName>catalogsearch</frontName>
modules controllers folder for a Check ResultController.php
Sunday, October 10, 2010
Magento Routing
/frontName/controllerName/actionName /catalogsearch/result/index If a Controller is found, Router checks if for
an action method
Mage_CatalogSearch_ResultController::indexAction
Front Controller Mage_Core_Controller_Varien_Router_Admin Looks at Global Cong and Instantiates Routers Mage_Core_Controller_Varien_Router_Standard Goes through each Router and calls match() until a controller is dispatched
match()
Builds a list of Moules with matching frontName Check each module until it nds a valid controller/ action pair
Magento Routing
Conguration base system, but a de-facto
convention exists
Custom Routing
Could add directly to your modules
<routers />
cong
controller_front_init_routers event
Practical Example
Create a custom Router class Add match rules to intercept URLs in the form
http://example.com/static-frontend/css/example.css
Adding A Router
Setting up an event listener Event handling class is also your Router
Implement Router
Extend base Router Class Implement Event Handling Method
class }
Sunday, October 10, 2010
Mage_Core_Controller_Varien_Router_Abstract
Alanstormdotcom_Staticroute_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract { public function initControllerRouters($observer) { //get reference to front controller /* @var $front Mage_Core_Controller_Varien_Front */ $front = $observer->getEvent()->getFront(); //add router $front->addRouter('arbitrary-name', $this); }
Implement match
Models
Getting out of the SQL Writing Business
MVC Models
MVC Model are classes that represent the
applications data
Also provide centralized location for query Encapsulate business login in methods
Sunday, October 10, 2010
Kohana Models
Extend from Base ORM class Mostly follows Active Record Pattern Create, Read, Update, Delete Simple Class Instantiation
Sunday, October 10, 2010
Magento Models
Many Models implement an Entity Attribute
Value store
Implement a Mapper pattern, making Model Client Programmer doesnt need to know
Sunday, October 10, 2010
Magento Models
Magento Replaces direction instantiation
with a indirect static method call
$user = new User(); $customer = Mage::getModel(cusotmer/customer);
Magento Collections
Collections are objects that act like arrays Ancestors implement Standard PHP Library Inherit from class Varien_Data_Collection Allows you to encapsulate business logic
with custom methods on collections Interfaces: IteratorAggregate and Countable
Views
It all comes back to HTML
MVC Views
Views create the response output Typically HTML Could be JSON, XML, etc. Separates template logic from application
Sunday, October 10, 2010
Kohana View
Controller
Contrller Interacts with Model based on request Controller Picks specic variables to send to view
Model
View
Kohana View
Traditional PHP MVC View Pattern Views are PHP mixed with HTML Often called Dumb View, since the View
Magento View
Controller
Contrller Interacts with Model based on request Controller Tells View to Kick Off
Model
View Reads Data Directly from Model Objects
View
Controller
Model
View Reads Data Directly from Model Objects
View
Controller
Controller Tells View to Kick Off Layout is a nested collection of recursively rendering blocks
Model
Layout
Root Block Block Block
Magento Blocks
The simplest block is just a PHP class with
a toHtml() method, which returns a string
Many Blocks render via a phtml template From within a phtml template, is a
$this
The phtml templates contain HTML and Block methods communicate with Models
Sunday, October 10, 2010
Block Class
Magento Layout
A single Layout object contains all the
Block objects for a particular request
which Blocks are available for any request Layout XML system allows designers and interactive developers to fully Layout XML system using PHP
Child Blocks
Sunday, October 10, 2010
The catalog/product_list Block has been added to the catalog/category_view Block, with the name product_list Allows us to use the following in catalog/category/view.phtml
$this->getChildHtml('product_list');
Action Nodes
Allow us to call methods on blocks from
XML Layout System