Request Flow

Is the process of taking a request and sending a response

  1. index.php initiates the Bootstrap
  2. Bootstrap loads App, implemented by Magento\Framework\App\Http
  3. App loads configuration
  4. Routing loops over routes to find a match
  5. Controller is page-specific
  6. Rendering generates the HTML
  7. Flushing Output pushes the HTML to the browser

Init

  • Sets up key objects such as index.php and Bootstrap
  • Entry points
    • index.php
    • pub/index.php
    • pub/cron.php
    • pub/static.php
    • shell

Routing

  • Defines available routers
  • Converts URLs into Magent-style ones
  • Parses request parameters
  • Identifies action classes

Routers

  • Routers handle certain URL types
  • The router defines its types itself
  • Responsible for identifying a URL pattern
  • A route is a module's pages, as defined in the module's etc/routes.xml
  • Routers are loaded in the following order:
    • Magento\Framework\App\Router\Base
    • Magento\Cms\Controller\Router
    • Magento\UrlRewrite\Controller\Router
    • Magento\Framework\App\Router\DefaultRouter
  • Magento\Framework\App\RouterInterface contains only public function match()
  • Adding a new Router is done by adding a new parameter to \Magento\Framework\App\RouterList using DI
  • Nonstandard routers are converted using the Request object's:
    • setModuleName($name)
    • setControllerName($name)
    • setActionName($name)
    • setParams($name, $value)

Controllers

  • execute() method is the entry point
  • Handles the request parameters
  • Starts the rendering process
  • Sometimes instantiates models
  • Parameters are available with $this->getRequest()->getParam('name')

Rendering

  • Includes templates within a class
  • Caches use the ob_* functions
  • Classes used:
    • Controller::execute() returns a result object
    • Router::match() returns a result object
    • FrontController::dispatch() dispatches a request and gets a result object
    • App::launch() copies the HTML to the result object
    • Bootstrap::run() flushes the result's HTML to the browser

Front Controllers

  • Gather routers injected using DI
  • Find matchinf routes/routers
  • Obtans HTML and populates the response object
  • Magento\Framework\App\FrontController implements Magento\Framework\App\FrontControllerInterface contains only public function dispatch()

URL Processing

  • magentoinstall.com/frontName/controller/action/params

Controllers

  • One controller handles only one action
  • Contains both execute() and a constructor for DI
  • Can be extended
  • Extends Magento\Framework\App\Action\Action > Magento\Framework\App\Action\AbstractAction
  • Magento\Framework\App\ActionInterface defines public function dispatch(ReuestInteface $request) and public function getResponse()
  • Is called by the dispatch() method, which calls execute()
  • Can be customised using either Preferences or Plugins
  • Result types:
    • Page (Magento\Framework\View\Result\Page) for HTML rendering
    • JSON (Magento\Controller\Result\Json) for returning JSON
    • Forward (Magento\Controller\Result\Forward) Load a different action without redirect
    • Redirect (Magento\Controller\Result\Redirect) Load a different action with redirect

Admin Controllers

  • Extend \Magento\Backend\App\Action > \Magento\Backend\App\AbstractAction
  • AbstractAction's dispatch() method checks _isAllowed()
  • Contains auxialiary methods _getSession(), _addBreadcrumb(), _addJs(), _addContent(), _addLeft(), _getUrl()

Creating Controllers

routes.xml:

1
2
3
4
5
6
/router/@id=standard
    route/
        @id -- route ID
        @frontName
        module/
            @name -- module name

frontName/subfolder/class

URL Rewrites

  • Make URLs simpler and easier to remember
  • Used on static, content, category and product pages
  • Stored in url_rewrite
  • Handled by Magento\UrlRrewrite\Controller\Router