how to generate a link to a module controller in prestashop?

Luca Reghellin picture Luca Reghellin · Aug 1, 2014 · Viewed 34k times · Source

What's the exact way to generate a link to a module controller in prestashop? Also, how should really be named the controller's class and how the url params should mirror?

Answer

Luca Reghellin picture Luca Reghellin · Aug 1, 2014

You will use an instance of the Link class. Generally you don't have to create one, just use context->link (ex. form a controller $this->context-link). The method is getModuleLink(), so:

$this->context->link->getModuleLink('module_folder_name','controller_name',array_of_params);

Beware the naming:

  • Module folder name is exactly that..

  • The controller must be in the right path, so for example module/controllers/front/controller.php

  • The file name is the action, lowercase. The class name is ModuleFolder+Action+"ModuleFrontController"

So, for example:

module dir: orderattachment

controller: orderattachment/controllers/front/pdf.php

controller class:

class OrderAttachmentPdfModuleFrontController extends ModuleFrontController

link:

$this->context->link->getModuleLink('orderattachments', 'pdf', [params..]);