Request Flow¶
Is the process of taking a request and sending a response
- index.php initiates the Bootstrap
- Bootstrap loads App, implemented by Magento\Framework\App\Http
- App loads configuration
- Routing loops over routes to find a match
- Controller is page-specific
- Rendering generates the HTML
- 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\RouterInterfacecontains onlypublic function match()- Adding a new Router is done by adding a new parameter to
\Magento\Framework\App\RouterListusing 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 objectRouter::match()returns a result objectFrontController::dispatch()dispatches a request and gets a result objectApp::launch()copies the HTML to the result objectBootstrap::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\FrontControllerInterfacecontains onlypublic 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\ActionInterfacedefinespublic function dispatch(ReuestInteface $request)andpublic function getResponse()- Is called by the
dispatch()method, which callsexecute() - 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
- Page (
Admin Controllers¶
- Extend
\Magento\Backend\App\Action>\Magento\Backend\App\AbstractAction AbstractAction'sdispatch()method checks_isAllowed()- Contains auxialiary methods
_getSession(),_addBreadcrumb(),_addJs(),_addContent(),_addLeft(),_getUrl()
Creating Controllers¶
routes.xml:
1 2 3 4 5 6 | |
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