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

What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

What’s really under the hood?

How I learned to stop worrying and love Magento

Monday, April 19, 2010

Page 2: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 3: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 4: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

How We Got HereA Brief History of Web Development

Monday, April 19, 2010

Page 5: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

HTML Page

Monday, April 19, 2010

Page 6: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

HTML Page

• Very radical shift

• No programming needed, just markup

• No proprietary file format, just ASCII text

• Limited interactivity, SSIs

Monday, April 19, 2010

Page 7: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

“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

Page 8: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 9: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 10: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 11: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

PHP Page

Monday, April 19, 2010

Page 12: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

Monday, April 19, 2010

Page 13: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

Monday, April 19, 2010

Page 14: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 15: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 16: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 17: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 18: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

Magento Routing

• URI: /frontName/controllerName/actionName

• URI: /catalogsearch/result/index

• No Standard Router!

Monday, April 19, 2010

Page 19: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 20: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

Monday, April 19, 2010

Page 21: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 22: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 23: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 24: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 25: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

Monday, April 19, 2010

Page 26: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 27: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 28: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

ModelsGetting out of the SQL Writing Business

Monday, April 19, 2010

Page 29: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

Kohana Models

• Extend from Base ORM class

• Mostly follows Active Record Pattern

• Create, Read, Update, Delete

• Simple Class Instantiation

Monday, April 19, 2010

Page 30: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

Monday, April 19, 2010

Page 31: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 32: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

Monday, April 19, 2010

Page 33: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

Monday, April 19, 2010

Page 34: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 35: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

ViewsIt all comes back to HTML

Monday, April 19, 2010

Page 36: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

Kohana ViewController

Model View

Contrller Interacts with Model based on request

Controller Picks specific variables to send to view

Monday, April 19, 2010

Page 37: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 38: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 39: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 40: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 41: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

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

Page 42: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

Block Class

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

Monday, April 19, 2010

Page 43: What’s really under the hood? - Alan Stormalanstorm.com/2010/pdf/Magento_Developers_Paradise.pdf · 2016-10-11 · Not a Computer Scientist • Many developers and programmers aren’t

How I learned to stop worrying and love Magento

What’s really under the hood?

Monday, April 19, 2010