Notice: You are browsing the documentation for PrestaShop 9, which is currently in development.
You might want to read the documentation for the current version, PrestaShop 8. Read the current version of this page
Hook | moduleRoutes |
---|---|
Locations | FO |
Type | |
Origin | core |
Aliases |
This hook has an $array_return
parameter set to true
(module output will be set by name in an array, see explaination here).
Origin | File |
---|---|
core | classes/Dispatcher.php |
Hook::exec('moduleRoutes', ['id_shop' => $id_shop], null, true, false)
This hook must return an array
of routes, keyed by the names of the routes, and each route structured by:
array(
'rule' => string,
'keywords' => array(),
'controller' => string,
'params' => array(
'fc' => string,
'module' => string
)
)
rule
and keywords
parametersrule
is the URL that will be matched by the router. rule
can contain parameters, enclosed with {}
.
For example, to add an id
parameter in your route, use the following rule
:
myrule/{id}
Multiple parameters can be used, for example:
myrule/{id}/{slug}
Each parameter in your rule
must be declared in the keywords
array.
In our example, id
must be an integer and slug
a string, and will be forwarded to the controller with id
and slug
parameter names:
[
'rule' => 'myrule/{id}/{slug}',
'keywords' => [
'id' => [
'regexp' => '[0-9]*',
'param' => 'id'
],
'slug' => [
'regexp' => '.*',
'param' => 'slug'
]
],
'controller' => 'myrulecontroller',
'params' => [
'fc' => 'module',
'module' => 'mymodulename'
]
]
This example creates two ModuleFrontController
controllers, and extends PrestaShop routes to map 2 routes to those controllers: one for listing items, the other one for showing one specific item.
Create a list.php
ModuleFrontController
in mymoduleaddingroutes/controllers/front/
:
class MyModuleAddingRoutesListModuleFrontController extends ModuleFrontController
{
public function initContent()
{
$this->setTemplate('module:mymoduleaddingroutes/views/templates/front/list.tpl');
}
}
and a show.php
ModuleFrontController
in mymoduleaddingroutes/controllers/front/
:
class MyModuleAddingRoutesShowModuleFrontController extends ModuleFrontController
{
public function initContent()
{
// It is just an example. Remember to always validate the input data!
$this->context->smarty->assign(
[
'id' => Tools::getValue('id'),
'slug' => Tools::getValue('slug')
]
);
$this->setTemplate('module:mymoduleaddingroutes/views/templates/front/show.tpl');
}
}
Now, create two templates in mymoduleaddingroutes/views/templates/front/
:
list.tpl
:
<h1>List template</h1>
show.tpl
:
<h1>Show template</h1>
Id: {$id}
Slug: {$slug}
Now we have 2 controllers, rendering 2 templates, we can create our routes with moduleRoutes
hook, in mymoduleaddingroutes/mymoduleaddingroutes.php
:
<?php
class MyModuleAddingRoutes extends Module
{
public function install()
{
return parent::install() && $this->registerHook('moduleRoutes');
}
public function hookModuleRoutes()
{
return [
'module-mymoduleaddingroutes-list' => [
'rule' => 'mymoduleaddingroutes/list',
'keywords' => [],
'controller' => 'list',
'params' => [
'fc' => 'module',
'module' => 'mymoduleaddingroutes'
]
],
'module-mymoduleaddingroutes-show' => [
'rule' => 'mymoduleaddingroutes/show/{id}/{slug}',
'keywords' => [
'id' => [
'regexp' => '[0-9]*',
'param' => 'id'
],
'slug' => [
'regexp' => '.*',
'param' => 'slug'
]
],
'controller' => 'show',
'params' => [
'fc' => 'module',
'module' => 'mymoduleaddingroutes'
]
]
];
}
}
The complete implementation example is available in our example modules repository.