Php 在prestashop中创建url重写模块
我正在尝试构建自己的自定义模块。这个模块非常简单。它需要读取url,如果访问者的url等于产品代码,则需要将用户重定向到特定的产品页面。为此,我使用以下url:Php 在prestashop中创建url重写模块,php,function,redirect,module,prestashop,Php,Function,Redirect,Module,Prestashop,我正在尝试构建自己的自定义模块。这个模块非常简单。它需要读取url,如果访问者的url等于产品代码,则需要将用户重定向到特定的产品页面。为此,我使用以下url: www.example.com/ean13/{ean13} 例如,当访问者试图访问页面时: www.example.com/ean13/1121312341 查询必须开始运行并搜索“ean13”产品代码。如果产品代码存在,则需要将用户重定向到特定产品页面 因此,我已经构建了模块的基础知识,目前的设置如下图所示: 如您所见,该模块仅
www.example.com/ean13/{ean13}
例如,当访问者试图访问页面时:
www.example.com/ean13/1121312341
查询必须开始运行并搜索“ean13”产品代码。如果产品代码存在,则需要将用户重定向到特定产品页面
因此,我已经构建了模块的基础知识,目前的设置如下图所示:
如您所见,该模块仅由两个文件组成。主模块配置文件“customRoute.php”和“controllers/front/routeController.php”中的控制器
以下两个文件的代码:
customRoute.php
if (!defined('_PS_VERSION_'))
{
exit;
}
class customRoute extends Module {
public function __construct()
{
$this->name = 'customRoute';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'Niels van Enckevort';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('custom routes');
$this->description = $this->l('Custom routes.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('customRoute'))
$this->warning = $this->l('No name provided');
}
public function install()
{
if (Shop::isFeatureActive())
Shop::setContext(Shop::CONTEXT_ALL);
if (!parent::install() ||
!$this->registerHook('ModuleRoutes') ||
!$this->registerHook('header') ||
!Configuration::updateValue('customRoute', 'my test')
)
return false;
return true;
}
public function uninstall()
{
if (!parent::uninstall() ||
!Configuration::deleteByName('customRoute')
)
return false;
return true;
}
public function hookDisplayHeader()
{
$this->context->controller->addCSS($this->_path.'css/mymodule.css', 'all');
}
public function hookModuleRoutes($params)
{
return [
'customRoute-customRouteRouteControllerModuleFrontController-root' => [
'rule' => 'ean13/{:ean13}/{rewrite}.html',
'controller' => 'routeController',
'keywords' => [
'ean13' => ['regexp' => '[0-9]+', 'param' => 'ean13']
],
'params' => [
'fc' => 'module',
'module' => 'customRoute'
]
]
];
}
}
class CustomRouteRouteControllerModuleFrontController extends moduleFrontController {
public function postProcess()
{
$query = new DbQuery();
$query->select('id_product')
->from('product_attribute', 'pa')
->where('pa.ean13 = ' . (int)Tools::getValue('ean13'));
$productId = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
if ($productId) {
Tools::redirect($this->context->link->getProductLink($productId));
} else {
Tools::redirect('pagenotfound');
}
}
}
routeController.php
if (!defined('_PS_VERSION_'))
{
exit;
}
class customRoute extends Module {
public function __construct()
{
$this->name = 'customRoute';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'Niels van Enckevort';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('custom routes');
$this->description = $this->l('Custom routes.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('customRoute'))
$this->warning = $this->l('No name provided');
}
public function install()
{
if (Shop::isFeatureActive())
Shop::setContext(Shop::CONTEXT_ALL);
if (!parent::install() ||
!$this->registerHook('ModuleRoutes') ||
!$this->registerHook('header') ||
!Configuration::updateValue('customRoute', 'my test')
)
return false;
return true;
}
public function uninstall()
{
if (!parent::uninstall() ||
!Configuration::deleteByName('customRoute')
)
return false;
return true;
}
public function hookDisplayHeader()
{
$this->context->controller->addCSS($this->_path.'css/mymodule.css', 'all');
}
public function hookModuleRoutes($params)
{
return [
'customRoute-customRouteRouteControllerModuleFrontController-root' => [
'rule' => 'ean13/{:ean13}/{rewrite}.html',
'controller' => 'routeController',
'keywords' => [
'ean13' => ['regexp' => '[0-9]+', 'param' => 'ean13']
],
'params' => [
'fc' => 'module',
'module' => 'customRoute'
]
]
];
}
}
class CustomRouteRouteControllerModuleFrontController extends moduleFrontController {
public function postProcess()
{
$query = new DbQuery();
$query->select('id_product')
->from('product_attribute', 'pa')
->where('pa.ean13 = ' . (int)Tools::getValue('ean13'));
$productId = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
if ($productId) {
Tools::redirect($this->context->link->getProductLink($productId));
} else {
Tools::redirect('pagenotfound');
}
}
}
我需要提到的是,我确实在一些帮助下获得了这段代码,因为这是我正在编写的第一个自定义模块。我想我遗漏了一个或多个关键项目,希望有人能帮我解决这些问题
模块安装和加载在前端,因此它与安装无关,而是与我正在使用的功能构建有关
如果您有任何问题,请在评论部分询问他们
一如既往,提前感谢 您的模块类是正常的,但控制器不是 我想之前已经回答过这个问题,但是模块前端控制器类必须声明如下:
类MyModuleNameControllerMemoduleFrontController扩展了ModuleFrontController
所以你的控制器应该是
class CustomRouteRouteControllerModuleFrontController扩展ModuleFrontController
编辑
问题是控制器名称和控制器文件名中有控制器
将routeController.php
重命名为route.php
,CustomRouteRouteControllerModuleFrontController
重命名为CustomRouteRouteModuleFrontController
,并将hookModuleRoutes
更改为
return [
'customroute-route-root' => [
'rule' => 'ean13/{:ean13}.html',
'controller' => 'route',
'keywords' => [
'ean13' => ['regexp' => '[0-9]+', 'param' => 'ean13']
],
'params' => [
'fc' => 'module',
'module' => 'customroute'
]
]
];
解决了这个问题,路由也能正常工作。是的,你做到了,从逻辑上说,这是正确的解决方案,但它仍然没有奏效。你能解释一下实现上述目标的全部步骤吗?我也用你的名字更新了我的代码。我大约在一小时前发现了这一部分。正如你在我前面的问题中提到的,我必须执行以下步骤:1。创建一个使用钩子ModuleOutes的模块,并配置一个友好的URL以使用模块控制器2。创建模块前端控制器在自定义控制器中运行db查询,检查是否存在具有请求的EAN的产品。如果产品存在,则重定向到产品页面,否则重定向到404页面或其他,,,,,,,因此,第一步:我已使用上面的代码创建了模块。并在我的模块的SEO部分添加了一个友好的url。步骤2,我模块的frontcontroller也在上面的代码中。以及第3步和第4步。是的,但是您的
hookModuleRoutes
与我在上一个问题中的回答不一样。另外,当您使用hookmoduleRoutes
时,不需要在后端的SEO部分配置任何内容。@abrfraTools::getValue('ean13')
应该可以工作。至少对于PS1.6,我已经两年没有接触过prestashop了,所以我不知道最新版本是什么。