Skip to content

Latest commit

 

History

History
57 lines (38 loc) · 3.2 KB

3. Demonstrate ability to customize request routing.md

File metadata and controls

57 lines (38 loc) · 3.2 KB

2.3 Demonstrate ability to customize request routing

Describe request routing and flow in Magento.

Frontend routers:

  • robots (10)

  • urlrewrite (20)

  • standard (30) - module/controller/action

    • get modules by front name registered in routes.xml
    • find action class
    • if action not found in all modules, searches "noroute" action in last module
  • cms (60)

  • default (100) - noRouteActionList - 'default' noroute handler

Adminhtml routers:

  • admin (10) - extends base router - module/controller/action, controller path prefix "adminhtml"
  • default (100) - noRouteActionList - 'backend' noroute handler

Default router (frontend and backend):

  • noRouteHandlerList. process

    • backend (10) Default admin 404 page "adminhtml/noroute/index" when requested path starts with admin route.

    • default (100) Default frontend 404 page "cms/noroute/index" - admin config option web/default/no_route.

  • always returns forward action - just mark request not dispatched - this will continue match loop

When is it necessary to create a new router or to customize existing routers?

Create new router when URL structure doesn't fit into module/controller/action template, e.g. fixed robots.txt or dynamic arbitraty rewrites from DB.

If you want to replace controller action, you can register custom module in controller lookup sequence - reference route by ID and add own module "before" original module.

How do you handle custom 404 pages?

  1. If front controller catches \Magento\Framework\Exception\NotFoundException, it changes action name "noroute" and continues loop. E.g. catalog/product/view/id/1 throws NotFoundException. catalog/product/noroute is checked.

  2. If standard router recognizes front name but can't find controller, it tries to find "noroute" action from last checked module. E.g. catalog/brand/info controller doesn't exist, so catalog/brand/noroute will be checked.

    \Magento\Framework\App\Router\Base::getNotFoundAction

  3. If all routers didn't match, default controller provides two opportunities: