How To Implement a Controller In a Custom Module

How To Implement a Controller In a Custom Module

Prestashop 1.5.x has treated us with a wonderful possibility to implement controllers in modules. This is the main goal of ModuleFrontController and ModuleAdminController classes. Let’s take a closer look at them (pay attention to comments):

 

Let’s now pass from theory to practice. For Prestashop to call Frontend controller right from the module folder, it’s necessary to create the controllers/front/mycontroller.php file in the module root directory.

Templates of your module can be located either in the root directory or here:

module_dir/views/templates/front/

Modulecontroller URL differs from front-end controllers and looks like this:

http://domain.com/index.php?fc=module&module=mymodule&controller=mycontroller



9 comments

  1. very interesting.

    How may I call a controller method as I used to do with

    ControllerFactory::getController(‘myController’)->myMethod();

    in version 1.4 ?

  2. Thanks for your help.

    I would like to call a controller method.
    So, do you know how to instaciate a controller with presta 1.5 ?
    In presta 1.4 I used ControllerFactory::getController(‘
    but it seems to work no more.

  3. Ademus,

    Try the following method:

    In a controller class:

  4. Hi there..

    I got a simple problem here but really need a solution.
    I made a module that can change colors for shop theme. it is functioning.

    Then i added an iframe in the admin form (form.tpl) so that anything change you may see the results instantly without have to click ‘view my shop’.

    so for the iframe, i use smarty variable {$base_dir} for it’s src.
    .

    it ends up with sowing nothing. Debugging the actual site, the iframe element have blank on the src value.

    And the question is, in the form.tpl, what is the right method to call any smarty variable? It seems that in the form.tpl doesn’t recognize the smarty variable. I thought that should be global ?

  5. Dudy,

    To output errors instead of a blank screen you need to follow recommendations described in this article.

    All the rest you are doing right, in any smarty template it is possible to use variables such as {$var}.

  6. Great explanation as usual, I work at PrestaShop but I still learn things on your website :)

    I would just add one useful piece of info: You can get the URL to this controller with the Link class like this:

    $this->context->link->getModuleLink(‘module_name’, ‘controller name’);

    This works from all objects that have a context: modules, other controllers, etc.

  7. Hi, excuse me for send you message for this form. I have a problem on creation the module on dashboard in my prestashop site. I need call a function of mymodule.php from an archive .tpl but only when I press function onclick the button. I think that you could help me. Thanks.

  8. Hello

    Can you please send me code for creating controller in admin folder of module by extending ModuleAdminController . I tried different ways but every time I get an error “Controller not found”.

    Regards
    Sandeep

Post a new comment

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