2016-08-18 32 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