Get Free Audit

Prestashop 1.7 MVC. Part 2: Creating a Controller

Jan 17, 2017

16676 Alexey Homiakov

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 tutorial we are going to elaborate the topic and will describe the steps to create a Prestashop controller.

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:

Igor Dragun
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Igor


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.

Looking for a quality PrestaShop solution? Find a suitable one at BelVG store.

Igor Dragun
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Igor

13 Comments

  1. Hi, Omar

    First, check if the friendly URLs are enabled. If yes, check the log file that you use (apache or nginx). The error is most likely not in the path but in the code.

  2. Hi,
    I made same steps to create a controller
    but when I run it : url/module/mymodule/mycontroller

    I get : 500 server error

    is any thing else we have to make other than making controller file and tpl file ?

  3. Hi,
    thank you for your feedback. You are right, technologies are changing.
    Our technical specialists have solved the problem, that prevented you from enjoying our blog and website.
    Hope, you will find a lot of useful information on our blog.
    Have a nice day!

  4. Fix the step scrolling that is on EVERY page of your website. It’s cancerous and shouldn’t be a thing in 2018.

    Your content seems great but i can’t stay one more minute on this website.

  5. hi,
    i followed you guide
    i have a new module cacontact and inside this module in ->controllers->front i created contact.php with this code:

    class cacontactContactModuleFrontController extends ModuleFrontController
    {

    public function initContent()
    {

    parent::initContent();
    $this->context->smarty->assign(array(
    ‘hello’ => ‘Hello World!!!’,
    ));
    //$this->setTemplate(‘module:cacontact/views/templates/front/display.tpl’);

    $this->setTemplate(‘display.tpl’);
    }

    }
    display.tpl is in views/templates/front
    but when i go to this url in my localhost:
    http://localhost:8888/prestashop/index.php?fc=module&module=cacontact&controller=cacontact

    give me a 404 error. why i can’t display the content in display.tpl?
    thanks a lot
    Piero

  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

  7. 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();

  8. 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
    }
    }
    }

  9. 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) {
    }

  10. 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

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

Post a new comment

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