Controllers

 

 

The controller layer consists of three systems: FrontController, Routing System and other controllers.Let's first create our own router class.Let's assume we already have some simple module, for example TestNamespace TestModuleNameAs you remember, to add a new module we just need to add the module.xml file and the registration.php file.To add a custom router class, we need to make some configuration changes in the di.xml file.To do this, let's take an example of di.xml files from the core Magento folder: vendor, magento, module-catalog.Let's remove all unnecessary dataNow we can add our configuration. To create a new router class, we need to add new items to the router list.To do this, we need to add our new router to the list of routers.The class "Magento Framework AppRouerList" is responsible for this.Let's add the necessary elements here.Here we need to specify the name of our new router.We also need to specify the type of this element.This must be an array.And inside this element we need to specify additional elements, such as the class name.We can also enable or disable this router.Finally, we need to specify the sort order of this router.And that's all.Now let's create a router class.Let's add some PHP code here.We need to return the action interface.Now we can add custom logic to the "match" method and the new router class will be added to the list of routers inside the front controller.Creating custom routers is a fairly rare task.So let's focus on the standard router and action classes instead.Let's create our controller. To do this, we need to declare a new route in the routes.xml file.Let's take an example from core module-catalog.As you can see, we need to specify the "router id" which is the default router, then we need to specify the "route id" which is actually the unique node identifier for this route in Magento, and the frontName which specifies the first segment after the base URL of the request URL.We also need to specify the module that will be responsible for this route.Let's specify something like "unique test route id" and some "special url key".Now we can add some action classes here.Let's add something like controller name "Test" and action name "Test".Let's add some PHP code here.The "execute" method in action classes contains the logic for sending requests.Magento assigns one controller to one request.It is not possible to process multiple URLs in one fileFor the following URL: special url key Test testThe controller name test will be the folder name and the action name Test will be the actual action classLet's add some additional information here.Let's clear the cache.Let's check the following URL: special_url_key test testWorks fine. The action class must return a result object, and it can be JSON, string data, some redirect, layout or page. Let's check them one by one.The first type we try is JSON, this type sets the content type to application JSON in the header and returns a JSON encoded representation of the array with the data. For this we need to use the result factory.Let's add some test data.Let's add this data to the result object.And let's return this result object.And let's check itWe can use JSON result when you need to return a JSON object. You can use this if you are implementing a custom API endpoint or a simple Ajax endpoint.The next type we'll try is row data."Row type" returns the data as it's been set, and it doesn't set a content type in the HeaderLet's say we need to return some XML data, so we can use the Row type here. For this, we need to specify some special header.It should be "Content Type: text xml".We also need to add some content.And let's return the result.Let's check itThe next type is "forward".Forward calls the "execute" method of another action class and does not start a new request from the browser.The URL will remain the same.For example, we have another controller, like this one here, and we need to show this result, but inside this action class.Here we have at least two options.We can use forward Method.We need to specify the action name, controller name and interface name.In this case it will be test_front_name.Let's check it.As you can see, the URL remains the same, but the result is from a different controller.We can also use the result factory.We can specify some special router.For example, 404 page.The next result object type is Redirect.The redirect type creates an external redirect that causes the browser to follow the link and request a new URL.Let's use the result factory.Let's set a new path and return the result.Note that 4O4 pages may be automatically cached, so let's clear the cache.And now we can check it.As you can see, the URL has been changed and redirected from some special key to test front name.We have two more types.These are layout and page types. These are layout and page types. The layout result type is very similar to the page result type. When returning a layout result, Magento still creates a response from the layout XML files, however, unlike a page result, the layout result type does not use Magento's "root" phtml template file.As a result, if you use the 'Layout' result type, you will get a blank page without any HTML Head and body tags.But if you select a page type, you will see at least a stylized blank page.Let's check it.Let's clear the cacheAnd let's check it.Now we have a blank page.But if we change the type to "page", then the phtml root template will be used.And as a result, you will see a stylized blank page.Let's clear the cache.And let's check it.Works fine.In most cases we will only use the page type.But what if we need to get some information from the request?In this case, we can use the request object.As you can see, we have a lot of useful methods here.For example, we can define the current module name, the current controller name, or the action name.And also we can get some value for a certain parameter or even all parameters as an array. Let's use the getParam method.In this case, we need to specify the key of the specific parameter and an optional default value, the default is Null.Let's use something like test param key.Let's specify some default valueLet's clear the cache.We have some default value here.And if we need to send some additional parameters, we can add them to the URL.We can also use other URL formatThis way you can get some information from the request and send some data to the browser.But what if you need to override the URL defined in the /vendor folder?In this case, the following approach can be used.You can add "before" or "after" parameters to a module node to override or extend routers.For example, you can add "before" here.Let's use something like Magento_CatalogThis configuration tells the Front Controller to look for actions in "VendorName_ModuleName" before looking in the "Magento_Catalog" module.This is how controllers work in Magento.
You have completed 0% of the lesson
0%