phpprestashopprestashop-1.6

URL link for custom module in Prestashop 1.6


I am currently developing a custom module. What I want is to have a nice URL, because right now it looks like this:

domain.com/flower-deliveries?city=Hamburg&id_country=1&country=Germany

I already added a new page to link to the custom module, the page name is flower-deliveries, but still I have the parameters that I have to "hide".

Instead, of that link above I would like a URL like this:

domain.com/flower-deliveries-1-Hamburg-Germany.html

I tried 2 methods, but none of them worked..

The first one, was to add a hookModuleRoutes in my controller, just like below:

public function hookModuleRoutes($params)
{
    return array(
        'module-vpages-dpage' => array(
            'controller' => 'dpage',
            'rule' => 'flower-deliveries{-:id_country}{-:country}{-:city}.html',
            'keywords' => array(
                'id_country' => array('regexp' => '[_a-zA-Z0-9_-]+', 'param' => 'id_country'),
                'city' => array('regexp' => '[\w]+', 'param' => 'city'),
                'country' => array('regexp' => '[\w]+', 'param' => 'country')
            ),
            'params' => array(
                'fc' => 'module',
                'module' => 'vpages',
                'controller' => 'dpage'
            )
        )
    );      
}

And then, in the controllers install:

$this->registerHook('moduleRoutes');

That didn't worked, so I tried to override the Dispatcher class, by adding a custom module route:

'module-vpages-dpage' => array(
    'controller' => 'dpage',
    'rule' => 'flower-deliveries{-:id_country}{-:country}{-:city}.html',
    'keywords' => array(
        'id_country' => array('regexp' => '[0-9]+', 'param' => 'id_country'),
        'city' => array('regexp' => '[\w]+', 'param' => 'city'),
        'country' => array('regexp' => '[\w]+', 'param' => 'country'),
    ),
    'params' => array(
        'fc' => 'module',
        'module' => 'vpages',
        'controller' => 'dpage'
    )
),

When using that custom rule, the link http://domain.com/flower-deliveries?city=Hamburg&id_country=1&country=Germany was tranformed in http://domain.com/flower-deliveries?module_action=list and it didn't worked and was redirecting me to the first page.

Could some one tell me what am I doing wrong? I've spent hours of reading how it should be done and it should be just like the ones above..

Thank you!


Solution

  • Revert all edits that you have done :).

    Try this way:

    For example, this is core module file rootofps/modules/vpages/vpages.php

    class VPages extends Module {
       public function __construct(){
           $this->name = 'vpages';
           $this->author = 'you';
           $this->tab = 'front_office_features';
           $this->version = '1.0.0';
           $this->controllers = array('dpage');
    
           parent::__construct();
       }
    
       // This is the function in your core module file (not in controller)
       public function install(){
           return parent::install() && $this->registerHook('moduleRoutes')
       }
    
       public function hookModuleRoutes($params){
          $my_link = array(
              'vpages' => array(
                  'controller' => 'dpage',
                  'rule' => 'flower-deliveries{-:id_country}{-:country}{-:city}.html',
                  'keywords' => array(
                      'id_country' => array('regexp' => '[0-9]+', 'param' => 'id_country'),
                      'country' => array('regexp' => '[\w]+', 'param' => 'country'),
                      'city' => array('regexp' => '[\w]+', 'param' => 'city'),
                   ),
                   'params' => array(
                       'fc' => 'module',
                       'module' => 'vpages'
                   )
               )
            );
            return $my_link;
        }
    }
    

    Now the controller rootofps/modules/vpages/controllers/front/dpage.php

    class VpagesDpageModuleFrontController extends ModuleFrontController {
        public function init(){
            parent::init();
            $this->setTemplate('dapage.tpl');
        }
    }
    

    And now the view rootofps/modules/vpages/views/templates/front/dpage.tpl

    id_country = {$smarty.get.id_country}<br>
    country = {$smarty.get.country}<br>
    city={$smarty.get.city}<br>
    

    This 'skeleton' works at 100% :), by the way, notice that if you give an url like this mydomain.com/flower-deliveries?id_country=1&country=italy&city=rome PrestaShop will not transform your url in a clearly url as you want. But an url like this mydomain.com/flower-deliveries-2-italy-rome.html will be routes properly :)