how to generate a link to a module controller in prestashop?
Solution 1
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..]);
Solution 2
An alternative way to create a link is to used an hyperlink in the view (template file) like the following:
index.php?fc=module&module=MODULE_NAME&controller=CONTROLLER_NAME
By example, in a ecommerce for car repair shop, the customer have to set the car he/she will have during the next appointment (the module hooked in the right column).
If his/her vehicle do not exists, he/she needs to create a new one (the front controller page we want to call).
In my example, in the view, the link would be:
<a href="index.php?fc=module&module=vehiclefile&controller=newvehicle">{l s='Create a new vehicle' mod='vehicleFile'}</a>
Note: As mentioned by Stratboy in his answer, the naming convention is very important otherwise Prestashop won't be able to find the page. By example,
class VehicleFileNewVehicleModuleFrontControlle extends ModuleFrontController
is missing the "r" of "controller" in the class name and produce the following error:
Note 2: I used this most excellent tutorial (with a complete concrete example) to get started in my own project : http://nemops.com/creating-new-pages-in-prestashop/#.VjpH2LerRhF
Luca Reghellin
I'm a full stack web developer since 1999. I'm an advanced html, css, javascript, php, wordpress and prestashop programmer and if needed, I can fully manage all projects sides, including design and art direction, and coordinate all the assets involved.
Updated on January 28, 2020Comments
-
Luca Reghellin over 4 years
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?
-
Sergio Antonio Snchez Camarero over 7 yearsHow to specify a name of controller if you haven't controller? I have this: '$this->context->link->getModuleLink('wul4pay', 'payment', [], true) ' But I haven't a controller
-
Luca Reghellin over 7 yearsHi, 'payment' should be a module controller: are you sure it's not? Have you checked the module's folder? Also, generally, saying 'link to a controller' is quite the same as 'link to a page'. I mean, a controller exist for a page. In other words, if you don't have a controller, than you don't have a dedicated page, so the question is: what are you trying to link then?
-
Flo over 6 yearsAnd for admin link: $this->context->link->getAdminLink(ControllerName)
-
Luca Reghellin over 5 yearshonestly, I don't know, I didn't tested 1.7 yet. But I guess you could easily find out by taking a look to the
Link
class. Just see ifLink::getModuleLink()
exists and if yes, take a look at the code. And/or eventually just try it. -
x29a over 5 yearsI tried with 1.7.5 and this is where im stuck: github.com/PrestaShop/PrestaShop/issues/12301
-
x29a over 5 yearsSee the linked issue, its working if you follow the instructions precisely
-
ubm over 4 years@LucaReghellin: I needs to create a link like this
index.php?fc=module&module=MODULE_NAME&controller=CONTROLLER_NAME&cat[]=4&cat[]=6&type[]=1&type[]=2
.How can i generate usinggetModuleLink
?