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.



7 comments

  1. Hello Dmitry,
    I have a question. How can I declare the action in the front controller and access it in the uri.

  2. Hi Tai,
    You can check the action in postProcess method of your controller. For instance:


    if (Tools::getValue(‘action’) == ‘displayUserProducts’) {
    $this->displayUserProductsAction();
    }

    It’s a typical action, don’t be afraid of it ;)

    In Prestashop the implementation of controller is not the same as in Zend url:/controller/action.
    in Prestashop it looks like: url:/module/name_module/name_controller

  3. Hi,

    I got a problem. I have controller in modules/coursemodule/controllers/admin/CourseModuleController.php. I want to send mail about order when access link like this admin798ip3jyf/index.php?controller=CourseModule&token=ec4415d8264c72494f776c35b2022f3b&action=notify&id_order=2&id_product=7

    0 && Tools::getValue(‘id_product’) > 0) {
    }

  4. Sorry I missclicked enter and I don’t know how to edit post.

    If i access link I only get “Page not found” and nothing happend. Can you please advice me :)

    P.S.: Sorry for my english, it’s not my native language.

    class CourseModuleController extends ModuleAdminController
    {

    public function postProcess()
    {
    parent::postProcess();
    if (Tools::getValue(‘action’) == ‘notify’ && Tools::getValue(‘id_order’) > 0 && Tools::getValue(‘id_product’) > 0) {
    send mail and do stuff
    }
    }
    }

  5. Have you registered the controller when installing the module?
    If not, you should register like this:

    $tab = new Tab();
    $tab->active = 0;
    $tab->class_name = 'CourseModule';
    $tab->name = array();

    foreach (Language::getLanguages(true) as $lang)
    $tab->name[$lang['id_lang']] = 'CourseModule';

    $tab->id_parent = 0;
    $tab->position = 0;
    $tab->module = 'module_name';

    $tab->save();

  6. What a begginer mistake. Thank you very much :). If possible…can you also advice me how to redirect back to this page?

    admin798ip3jyf/index.php/product/form/7?_token=LjlFuEceTViJO6smHqiIUOaw6A9mbKdpCGD_QFrRI50#tab-hooks

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *