MVC overview

 

 

Magento uses its own version of the MVC pattern.It uses XML to control application configuration and actions on top of the regular model view controller architecture.Additionally, Magento uses the front controller pattern to implement the workflows of its application.It has one entry point, such as the index.php file, for all web requests.So let's see how it all works.ModelView ControllerThe model is the part of the application that works with the stored data. Magento uses a resource model, models and collections.For example, we have a test table with some data; in order to work with this data, we need to create at least three files.Model, Resource model and Collection.In the model, we must specify the resource model. In resource model we have to specify table name and id column and in collection we have to specify model and resource model.So now we can use the test table data in our code.View.Let's enable template hints in the Magento admin panel and check the debugging information in the storefront.To do this, we need to go to the Magento admin panel.Let's open Stores -> configurationAdvanced -> developerAnd in the Debug section, we need to enable template hints for the storefront and possibly add a block class type.So now we can use a GET URL parameter like templatehints.Let's add this GET parameter to our URL.As you can see, we have some structure of phtml templates and PHP classes.Templates in Magento are phtml files that can contain HTML instructions."Blocks" allow you to move reusable functions from PHP template files into classes. This way they can be reused for other template files in the future.Actually, according to Magento best practices, we need to use "view models" instead of blocks whenever possible, but we'll discuss that later.So let's take a look.On the Blocks.For example.HTML titleAs you can see, we have some HTML code and we can get the information we need from the Block class. A block is a class that calls a template and provides data to that template.Blocks often instantiate models, which can then query databases, etc.As you can see, this block and phtml file are mentioned in the Layout XML file.A layout XML file is an XML file in which you can define the structure of a page using XML statements.So we have phtml templates, block classes for them and page structure in XML layout.Controller.The controller layer consists of three systems.Front controller, routing system and other controllers.The application front controller is responsible for sending application requests. The front controller contains logic common to all actions.Each application area has its own front controller. The Front Controller class searches the list of routers provided by the router list class until it finds one that can handle the request.When the front controller finds a matching router, it sends a request to the action class returned by the router.If the front control cannot find a router to process the request, it uses the default router.Magento 2 assigns one controller to one request.It is not possible to handle multiple URLs in a single file.Let's add a test controller, just to understand how it works.To do this, we need to add our URL or name to the standard router class and create an action class for the specific URL.In the controller folderThat's it.Now we can test our own controller.Works fine.Great!
You have completed 0% of the lesson
0%