In this blog, we are going to learn how to show custom URL in PrestaShop using moduleRoutes hook. With the help of moduleRoutes hook we can also create custom rules for module controller.
If module is using front controller then it generates URL’s according to the below rule
module/{module}{/:controller}
For instance, the controller demo of the module demo_module
will have the address:
module/demo_module/demo
In above image you can see module name and controller are visible in url. But in Prestashop there is a way to hide the original address of the controller with the help of the hook mоduleRоutes
.
Prestashop will use this pattern to compare addresses: module-{module_name}-{controller_name} public function hookModuleRoutes() { return array( 'module-demo_module-demo' => array( 'controller' => 'demo', //front controller name 'rule' => 'mypage', //the desired page URL 'keywords' => array( 'link_rewrite' => array( 'regexp' => '[_a-zA-Z0-9-\pL]*', 'param' => 'link_rewrite' ), ), 'params' => array( 'fc' => 'module', 'module' => 'demo_module', //module name ) ), ); }
Using above method, we generate custom URL in PrestaShop using moduleRoutes hook as https://{your_site}/mypage
When ever we open demo_module -> demo
controller then the URL will be as https://{your_site}/mypage
as shown in above image.
Dispatcher::loadRoutes(),
is responsible for loading patterns, will load your custom rule before other rules, therefore the custom rule will have a higher priority than the predefined PrestaShop rule.
That’s all.
If you are facing any issues or doubts in the above process, please feel free to contact us through the comment section.
I would be happy to help.
Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.
For any doubt contact us at [email protected].
Be the first to comment.