2016-08-18 70 views
2

我目前正在开发一个自定义模块。 我要的是有一个很好的网址,因为现在它看起来像这样:Prestashop中的自定义模块的URL链接1.6

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

我已经添加了一个新的页面链接到自定义模块,页面的名称是花交付,但我仍然有我必须“隐藏”的参数。

相反,该链接的上面,我想这样的URL:

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

我试过2种方法,但他们没有工作..

第一个,就是增加一个hookModuleRoutes在我的控制器,就像下面:

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' 
      ) 
     ) 
    );  
} 

然后,在控制器上安装:

$this->registerHook('moduleRoutes'); 

这并没有工作,所以我试图重写Dispatcher类,加入了自定义模块路线:

'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' 
    ) 
), 

当使用自定义规则,链接http://domain.com/flower-deliveries?city=Hamburg&id_country=1&country=Germanyhttp://domain.com/flower-deliveries?module_action=list被tranformed,它没”没有工作,并将我重定向到第一页。

有人能告诉我我做错了什么吗? 我已经花了数小时阅读它应该如何完成,它应该就像上面的那些。

谢谢!

+0

您的第一种方法是可以的,但它需要在您的模块类中,而不是在控制器中。在你的规则中,你已经定义了'rule'=>'flower-deliveries { - :id_country} { - :country} { - :city} .html' -Germany'。您在国家之前使用城市,但按照不同的顺序定义其规则。 – TheDrot

+0

是的,我知道,我修改了模块,在模块文件中有钩子,但仍然没有任何内容..它不显示为假设.. – rosuandreimihai

+0

定义您的规则,就像这个''rule'=>'flower-deliveries- {:id_country} - {:country} - {:city} .html''并查看它是否有效。我测试了你的第一个解决方案,然后PrestaShop抛出异常,但是这种方式对我很有用。 – TheDrot

回答

5

恢复您所做的所有修改:)。

试试这个方法:

例如,这是核心模块文件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; 
    } 
} 

现在控制器rootofps/modules/vpages/controllers/front/dpage.php

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

而现在的观点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> 

这个'骨架'在100%:)工作,顺便说一下,请注意,如果你给这样的网址mydomain.com/flower-deliveries?id_country=1&country=italy&city=rome PrestaShop不会将你的网址变成一个清晰的网址,只要你想。 但是像这样的网址mydomain.com/flower-deliveries-2-italy-rome.html将正确路由:)

+0

嗨,我已经在你的答案中尝试过,但它根本不起作用..我仍然有丑陋的url链接.. :( – rosuandreimihai

+0

检查数据库是否是你以前的测试“脏”(ps_meta,ps_meta_lang ),删除调度员的任何覆盖 – sarcom

+0

我没有任何重写关于这个话题,所以它应该是现在干净..但我不明白为什么它不工作.. – rosuandreimihai