Prestashop 1.7 MVC. Part 2: Creating a Controller

Prestashop 1.7 MVC. Part 2: Creating a Controller

In one of our previous articles we reviewed the process of creating a model in Prestashop MVC.  In this article we are going to elaborate the topic and will describe the steps to create a controller in Prestashop.

The process of creating a controller in Prestashop is based on 4 classes:

  1. Dispatcher
  2. Controller
  3. FrontController
  4. AdminController

These classes are located under /classes/(Dispatcher) and /classes/controller/* directories.

  1. Dispatcher is responsible for website navigation as well as for selection of controller either from kernel or from module. The dispatching process in based on the following:

Front end:

  1. http://example.com/index.php?id_category=3&controller=category
  2. http://example.com/3-category_name
  1. http://site_name.com/index.php?id_product=7&controller=product
  2. http://example.com/7-product_name.html

Back end:

  1. http://example.com/admin-dev/index.php?controller=AdminCategories
  2. http://example.com/admin-dev/index.php?controller=AdminProducts

Module pages:

  1. http://example.com/index.php?fc=module&controller=module_controller
  2. http://example.com/module/module_name/controller_name

The second list of links will work only if the Friendly URL option is enabled.

The most informative code is located in the dispatch() method:

Controller is selected from the property $default_routes on the basis of a regular expression if the Friendly URL option is enabled. Thereafter the run() method is called.

  1. Controller:

The run() method defines the behavior of the controller:

The code is quite simple: the init() method initializes the controller,  checkAccess() verifies the access,  setMedia() adds js, css files to the page,  postProcess processes ajaxProcess(),  initHeader() initializes page header,  initContent() outputs the content part,  initFooter() initializes page footer,  display() – renders the html code of the page.

  1. FrontController, AdminController inherit the behavior of the Controller class and modify it in accordance with their needs.

But we are mostly interested in ModuleAdminController and ModuleFrontController because they inherit the behavior of the AdminController and FrontController classes with some minor changes and we are going to use them quite a lot when creating new modules.

To create a custom ModuleFrontController class you need to follow these steps:

  1. create a file with the same name as the controller
  2. save it to the folder /modules/name_module/controllers/front/name_controller.php
  3. the controller class name should consist of three parts name_moduleName_controllerModuleFrontController

class mymoduleMycontrollerModuleFrontController extends ModuleFrontController{}

To output the content we will need to override the method initContent() which we have encountered in the run() method of the Controller class:

Here we transmit the variable ‘hello’ into the template file which should be located under: /module/name_module/views/templates/front/template_name.tpl

To create a custom ModuleAdminController class you will need to:

  1. create a file with the same name as the controller
  2. save it to the folder /modules/name_module/controllers/admin/AdminExampleController.php
  3. the file should have the same name as the class – AdminExampleController

The class AdminExampleController extends ModuleAdminController{}

It can contain the logic which will control the behavior of the module and its content.

To register the controller in the admin part of the website:

There is no need to connect the template because in most cases it is quite enough to use helpers. In the first article we described how a controller interacts with a model in Prestashop.

By default we use the method renderView() to output content in the admin part:

Today we have reviewed the operation principle of a dispatcher, analyzed cms controller structure, the inheritance hierarchy and its basic code, analyzed the possibility of creating a custom controller for a module.



Post a new comment

top