Request flow

 

 

The entry point for standard Magento requests is index.php for development mode or pub/index.php for production mode. It can also be cron.php for cron jobs or bin/magento for shell scripts.Let's take a look inside the index.php file.Magento first checks the bootstrap file. The bootstrap file is responsible for subsequent loading and initial launch of the application.After this, bootstrap creates and runs the applicationso let's check it.In bootstrap we have the basic initialization components like setting up a custom error handler, checking if maintenance mode is enabled, checking if Magento is installed.By the way, if you delete the app e t c env.php file, Magento will ask you to install the app.So if you need to reinstall Magento, you will have to delete this file first.After this, the application is launched.There are several types of applications in Magento 2.For example: HTTP for standard pages and Cron for running scheduled cron jobsIn this case we will only check the Http application.As you can see here, Magento detects the area by URL and sets the object manager accordingly.After this, the front controller will be initialized and dispatched.Let's check it.In this case FrontController will be used.The FrontControler class searches the list of routers provided by the _routerList class until it finds one that can handle the request.When the front controller finds a matching router, it sends the request to the action class returned by the router. If the front controller cannot find a router to handle the request, it uses the default router, which will return the configured 404 page by default.The router class passes the request to the action class, which handles the request.As you can see, we have six routers by default. They are: CMS Router, Standard Router, Default Router, Robots txt Router, UrlRewrite Router, and Web API Router.The first router in the queue will be the Robots txt router, this router matches the request with the robots txt file.You can edit the robots txt file in the admin panel so that it can be generated on the fly.We can check this in the admin panel under Content -> Design and Configuration.We can define this on a global level.Let's do this.Here we have a tab called Search Engine Robots and we can reset its settings to default values.Let's check the current robots txt file.As you can see, the current robots.txt file is empty, so let's reset it to default values ​​and save the changes.Now we see a new robots.txt file.But if we check the files in the project, we will not see the robots.txt file, since it will be generated automatically.Next is the UrlRewrite router, which follows right after the Robots router. UrlRewrite matches the request with a URL defined in the database.We can create custom redirects from one URL to another in the admin panel, so such redirects will be intercepted here.We can check this in the admin panel under Marketing -> URL Rewrite.As you can see, all the products have SEO friendly aliases, so if you try to use the SEO friendly URL, you will actually see the result of the following model, controller, and action, as well as additional parameters such as the id and value of that parameter.You can also define the redirect type.By default, no new request is sent from the browser, so the URL remains the same.But if you need to perform an external redirect that the browser follows and requests a new URL, then you can use some types of redirects here.Let's create a redirect from one custom URL to another.For example, for the "About Us" page.First we need to define the request path.For example, some test url key.The target path could be "about-us" and let's add a 301 redirect type. And we can add some optional description.Let's save this.Let's check our new some_test_url_key.Great.Works fine.After the UrlRewrite router, Magento will try to find a result using the Standard router. A URL using the standard router has the following format:front_namecontroller nameand action name.A standard router parses this URL format and matches it to the correct controller and action.The Front name points to a specific module that we can define in routes.xml.The controller name specifies the name of the controller, which is the name of the Controller subfolder in the specified moduleand the action name specifies the action class that is responsible for the response.As you probably remember, we created a custom controller in app code VendorName ModuleNameWe added an additional name to routes.xml and added an Index actionAs a result, we can try to find this particular action class by the following URL pathtest_front_name index - the name of the controllerindex - the name of the action Works fineIf the standard router can't find any action class for that URL, Magento will try to use the CMS router.CMS router matches the request for CMS pages because each CMS page can have its own URL key defined in the admin panel, so Magento uses a separate router class to handle such URLs.Let's look at such a URL.We can find them in content pages.Each page has its own URL key.You can find it here."search engine optimization"As you can see, we can change the URL key for any custom page at any time, and to handle such changes, Magento has added a separate router for CMS pages.If we don't have any Web API calls, the default router will be invoked. The default router, defined by the DefaultRouter class, is the last router Magento checks during the routing process.These are usually invalid URLs that the previous Reuters cannot handle. By default, this router will return a custom 404 page, which can be changed in the admin panel.You can create as many custom routers as you need, but in most cases you can use a standard router.After this, Magento returns the result and closes the connection.Let's check the same flow but through XDebug for our custom action class.As I said before, Magento first checks the bootstrap file after this bootstrap class creates and starts the application.Inside the run method we have initErrorHandler, assertMaintenance, assertInstalledAnd after that the actual application launch occurs.Inside the launch method Magento defines the area code.In our case it will be Frontend.Magento will then configure this scope and we will configure the object manager according to this scope.The front controller will then be initialized.And the Front Controller method will be executed.Inside this front controller dispatch method, Magento looks at the list of routers provided by the router class until it finds one that can handle the request.Now we have a Robots txt router.Magento could not find any matches in the Robots router.Next comes the UrlRewrite router, and Magento can't find any matches for this router either.Now we have a basic or standard router.A standard router parses a special URL format such as frontname, controller name, and action name and matches it to the correct controller and action.Let's take a look at this code. As you can see, we have a matchAction inside the standard router, and then we have some specific action class name that will be responsible for this request.Now we have some instance of the action.Magento will try to use this action instance to handle the request.Inside the process request, the dispatch method of this action instance will actually be executed, and then the execute method of the action class will be executed.That's it.After that, Magento will return the response result, and then Magento will close the connection.
You have completed 0% of the lesson
0%