ros*_*hai 2 php prestashop prestashop-1.6
我目前正在开发一个自定义模块.我想要的是拥有一个不错的URL,因为现在它看起来像这样:
domain.com/flower-deliveries?city=Hamburg&id_country=1&country=Germany
Run Code Online (Sandbox Code Playgroud)
我已经添加了一个新页面来链接到自定义模块,页面名称是鲜花交付,但我仍然有我必须"隐藏"的参数.
相反,在上面的链接中,我想要一个这样的URL:
domain.com/flower-deliveries-1-Hamburg-Germany.html
Run Code Online (Sandbox Code Playgroud)
我尝试了两种方法,但没有一种方法有效.
第一个是在我的控制器中添加一个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'
)
)
);
}
Run Code Online (Sandbox Code Playgroud)
然后,在控制器安装中:
$this->registerHook('moduleRoutes');
Run Code Online (Sandbox Code Playgroud)
这没用,所以我尝试通过添加自定义模块路由来覆盖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'
)
),
Run Code Online (Sandbox Code Playgroud)
使用该自定义规则时,链接http://domain.com/flower-deliveries?city=Hamburg&id_country=1&country=Germany已转换为http://domain.com/flower-deliveries?module_action=list无效,并将我重定向到第一页.
有人可以告诉我,我做错了什么?我花了几个小时阅读它应该如何完成,它应该像上面的那样.
谢谢!
恢复您已完成的所有修改:).
试试这种方式:
例如,这是核心模块文件 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;
}
}
Run Code Online (Sandbox Code Playgroud)
现在是控制器 rootofps/modules/vpages/controllers/front/dpage.php
class VpagesDpageModuleFrontController extends ModuleFrontController {
public function init(){
parent::init();
$this->setTemplate('dapage.tpl');
}
}
Run Code Online (Sandbox Code Playgroud)
而现在的观点 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>
Run Code Online (Sandbox Code Playgroud)
顺便提一下,这个"骨架"工作在100%:),请注意,如果你给这样的网址,mydomain.com/flower-deliveries?id_country=1&country=italy&city=romePrestaShop不会根据你的需要在你的网址中转换你的网址.但是像这样的网址mydomain.com/flower-deliveries-2-italy-rome.html将是正确的路线:)