what’s really under the hood? - alanstorm.com · not a computer scientist • many developers and...

43
What’s really under the hood? How I learned to stop worrying and love Magento Monday, April 19, 2010

Upload: trandang

Post on 16-Jun-2019

221 views

Category:

Documents


0 download

TRANSCRIPT

What’s really under the hood?

How I learned to stop worrying and love Magento

Monday, April 19, 2010

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 ...

Monday, April 19, 2010

Not a Computer Scientist

• Many developers and programmers aren’t

• You don't need to be a computer scientist to work with Magento

• Magento is similar to PHP Frameworks (Cake, CodeIgniter, Kohana), just deeper

• Magento and Kohana, side by side

• But first

Monday, April 19, 2010

How We Got HereA Brief History of Web Development

Monday, April 19, 2010

HTML Page

Monday, April 19, 2010

HTML Page

• Very radical shift

• No programming needed, just markup

• No proprietary file format, just ASCII text

• Limited interactivity, SSIs

Monday, April 19, 2010

“Scripting” vs. “Real Programming"• HTML included markup for form elements

• Stealth runtime, with client/server approach

• “Real Programmers” could do everything in C, C++, Java, lisp

• “Scripters” could piggy back on the web server for HTTP and produce HTML

Monday, April 19, 2010

Perl/CGI

• Perl was once the new hotness

• Excelled as string processing, and Web applications are strings over HTTP

• CPAN meant a shared code library

• Non-complied meant distributed programs were shared in the open

Monday, April 19, 2010

Perl/CGI

• CGI was slow, and FastCGI too new

• Perl didn't scale way before ruby didn't

• CPAN’s good but no standard library for the growing shared hosting ecosystem

Monday, April 19, 2010

PHP3/PHP4

• Looks like Perl! Sort of.

• Build important features directly into language, distributed by default

• Could import C code as extensions

• The build-up/tear-down approach of mod_php ideal for shared hosts

Monday, April 19, 2010

PHP Page

Monday, April 19, 2010

Monday, April 19, 2010

Monday, April 19, 2010

Rise of PHP Systems

• Created to combat PHP Applications becoming too complex/messy

• Shopping Cart Applications

• CMS systems (Drupal, Mambo, Joomla)

• Programming Frameworks, many inspired by Ruby on Rails (CakePHP, Code Igniter, Symfony)

Monday, April 19, 2010

Magento is the Next Evolutionary Step

• Is a shopping cart application

• Uses a module based approach to load new code into the system, similar to Drupal

• Implements a configuration based MVC

• Most PHP Frameworks are convention based, Magento is configuration based

Monday, April 19, 2010

Controllers and Routing

A Web MVC Router transforms the request URL into a controller class name and decides which method on

that controller is the entry point

Monday, April 19, 2010

Kohana Routing

• Routing handled by Router_Core::setup

• URI: /controllerName/actionName

• URI: /welcome/index

• Welcome_Controller::index();

• File: /application/controllers/welcome.php

Monday, April 19, 2010

Magento Routing

• URI: /frontName/controllerName/actionName

• URI: /catalogsearch/result/index

• No Standard Router!

Monday, April 19, 2010

Magento Routing

• Magento Searches Global Config for routers

• web/routers

• Default system ships with two Routers

• Mage_Core_Controller_Varien_Router_Admin

• Mage_Core_Controller_Varien_Router_Standard

• Configured in Mage_Core

Monday, April 19, 2010

Monday, April 19, 2010

Magento Routing

• The match method is called on each router

• The match method is responsible for

• Creating a Controller class

• Creating an Action method

• Calling the Action method

Monday, April 19, 2010

Magento Routing

• URI: /frontName/controllerName/actionName

• URI: /catalogsearch/result/index

• Default routers will

• Gather any module with a configured <frontName>catalogsearch</frontName>

• Check modules controllers folder for a Packagename_Modulename_ControllerName Mage_CatalogSearch_ResultController

Monday, April 19, 2010

Magento Routing

• /frontName/controllerName/actionName

• /catalogsearch/result/index

• If a Controller is found, Router checks if for an action.

• Mage_CatalogSearch_ResultController::indexAction

• If action is found, it’s called, and the module search and match search stops.

Monday, April 19, 2010

Magento Routing

• Most of the time in a fresh install

• /catalogsearch/result/index

• translates to

• Module: Mage_CatalogSearch

• Controller: ResultController

• Action: indexAction

Monday, April 19, 2010

Monday, April 19, 2010

Magento Routing

• Possible to Add Additional Routers through event system

• controller_front_init_routers

• See

• Mage_Cms_Controller_Router::initControllerRouters

• Sets up Mage_Cms_Controller_Router with different match rules

Monday, April 19, 2010

Mage_Core_Controller_Varien_Router_Admin

Mage_Core_Controller_Varien_Router_Standard

Front Controller

Looks at Global Config and Instantiates Routers

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 finds a valid controller/

action pair

Monday, April 19, 2010

ModelsGetting out of the SQL Writing Business

Monday, April 19, 2010

Kohana Models

• Extend from Base ORM class

• Mostly follows Active Record Pattern

• Create, Read, Update, Delete

• Simple Class Instantiation

Monday, April 19, 2010

Monday, April 19, 2010

Magento Models

• Most Complex Models implement an Entity Attribute Value store

• Simpler “basic” models offers Active Record like functionality

• Implement a Mapper pattern, making Model independant of data store

• Client Programmer doesn’t need to know these details to use.

Monday, April 19, 2010

Monday, April 19, 2010

Monday, April 19, 2010

Magento Models

• Magento Replaces direction instantiation with a indirect static method call

• $user = new User();

• $customer = Mage::getModel(‘cusotmer/customer’);

• Every model has a Collection object which is used when fetching for multiple models. Kohana and other frameworks, return PHP arrays()

Monday, April 19, 2010

ViewsIt all comes back to HTML

Monday, April 19, 2010

Kohana ViewController

Model View

Contrller Interacts with Model based on request

Controller Picks specific variables to send to view

Monday, April 19, 2010

Kohana View

• Traditional PHP MVC View Pattern

• Views are PHP mixed with HTML

• Often called “Dumb View”, since the View doesn’t know anything about the Application

Monday, April 19, 2010

Magento ViewController

Model View

Contrller Interacts with Model based on request

Controller Tells View to Kick Off

View Reads Data Directlyfrom Model Objects

Monday, April 19, 2010

Controller

Model View

Contrller Interacts with Model based on request

Controller Tells View to Kick Off

View Reads Data Directlyfrom Model Objects

Monday, April 19, 2010

Controller

Model Layout

Contrller Interacts with Model based on request

Controller Tells View to Kick Off

Root Block

Block

Block

Block

Block

Block

Blocks Read Data Directlyfrom Model

Objects

Layout is a nested collection of recursively rendering blocks

Monday, April 19, 2010

Magento Block

• The simplest block is just a PHP class with a toHtml(), which returns a string

• Many Blocks render via a phtml template

• From within a phtml template, $this is a reference to the containing Block

• The phtml templates contain HTML and PHP code for rendering logic

• Block methods communicate with Models

Monday, April 19, 2010

Block Class

Template for BlockTemplates may be set programmatically or via the Layout XML files

Monday, April 19, 2010

How I learned to stop worrying and love Magento

What’s really under the hood?

Monday, April 19, 2010