【发布时间】:2018-03-22 14:12:44
【问题描述】:
我正在尝试构建自己的自定义 prestashop 模块。该模块非常简单。它需要读取 url,如果访问者的 url 等于产品代码,它需要将用户重定向到该特定产品页面。为此,我使用以下网址:
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'
]
]
];
}
}
routeController.php
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');
}
}
}
我需要提一下,我确实在一些帮助下获得了此代码,因为这是我正在编写的第一个自定义模块。我想我缺少一个或多个关键项目,希望有人可以帮助我解决这些问题。
模块是在前面安装和加载的,所以它与安装无关,但与我正在使用的功能构建有关。
如果您有任何问题,请在 cmets 部分提问 一如既往,提前致谢!
【问题讨论】:
标签: php function redirect module prestashop