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?
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..]);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With