Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 442
Luca Reghellin Avatar asked Aug 01 '14 16:08

Luca Reghellin


2 Answers

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..]);
like image 121
Luca Reghellin Avatar answered Sep 19 '22 14:09

Luca Reghellin


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:

enter image description here

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

like image 39
Jonathan Parent Lévesque Avatar answered Sep 20 '22 14:09

Jonathan Parent Lévesque