Jump to content

Create a Custom route to a controller in custom Module. Prestashop 1.7.6x


Recommended Posts

Hello can i please can anyone help me with clear example of a working custom route to a controller in a custom module? the tutorial on that is quite unclear an i am still unable to create a working route to a controller in the module i am building.

Thanks. 

  • Like 1
Link to comment
Share on other sites

public function hookModuleRoutes($params)
    {
        $main = Configuration::get('some_config', $this->language->id);
        $my_routes = array(
            'module-[your_module_directory]-[some_distinct_name]' => array(
                'controller' => '[your_custom_controller]',
                'rule' => $main . '/{link_rewrite}',
                'keywords' => array(
                    'link_rewrite' =>   array('regexp' => '[_a-zA-Z0-9-\pL]*', 'param' => 'link_rewrite'),
                ),
                'params' => array(
                    'fc' => 'module',
                    'module' => '[your_module_directory]',
                ),
            )
        );

        return $my_routes;
    }

If for example your module is cjordan and your controller banana then the above code becomes

public function hookModuleRoutes($params)
    {
        $main = Configuration::get('some_config', $this->language->id);
        $my_routes = array(
            'module-cjordan-banana' => array(
                'controller' => 'banana',
                'rule' => $main . '/{link_rewrite}',
                'keywords' => array(
                    'link_rewrite' =>   array('regexp' => '[_a-zA-Z0-9-\pL]*', 'param' => 'link_rewrite'),
                ),
                'params' => array(
                    'fc' => 'module',
                    'module' => 'cjordan',
                ),
            )
        );

        return $my_routes;
    }


and of course transplant the module in the respective hook

$this->registerHook('moduleRoutes');

Just keep in mind that when calling some custom link the controller is not the "controller" but the last part of the ”module-cjordan-banana”. In this case it's the same, but in complex situations we can have for example one controller with more routes pointing there.

It can be for example category_name_asc the last part in a blog module but category the controller,
which means display the category X with its post ordered by name in ascending order.
So in this case in the link call you put category_name_asc instead of category.

Moreover, take care of the redirection when changing language. Because it does not redirect automatically.

Regards,
Konstantinos - a passionate PrestaShop fan.

  • Like 2
  • Haha 1
Link to comment
Share on other sites

  • 1 month later...
  • 1 year later...

@pbeu

The value means if you like to set any configuration value which need to change like in our case we use smart log/category as prefix of all URL. But some one like blog/category

In this case blog value come from that configuration value. For more research you can ready our smart log code from git.

 

You will find no other big prestashop module like smartblog as free.

 

Thank you

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...