【问题标题】:Creating a url rewrite module in prestashop在 prestashop 中创建一个 url 重写模块
【发布时间】: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


    【解决方案1】:

    你的模块类没问题,但你的控制器不行。

    在我想之前我已经回答了这个问题,但是模块前端控制器类必须声明如下:

    class MyModuleNameControllerNameModuleFrontController extends ModuleFrontController

    所以你的控制器应该是

    class CustomRouteRouteControllerModuleFrontController extends ModuleFrontController

    编辑

    问题是控制器名称和控制器文件名中有Controller

    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. 创建一个使用钩子 moduleRoutes 的模块并配置一个友好的 URL 以使用模块控制器 2. 创建一个模块前端控制器在您的自定义控制器中运行 db 查询以检查是否存在具有请求的 EAN 的产品 如果产品存在,则重定向到产品页面,否则重定向到 404 页面或其他东西 ,,,,, 所以第一步:我已经使用上面的代码创建了模块.并在我的模块的 SEO 部分添加了一个友好的 url。第二步,我的模块的前端控制器也在上面的代码中。以及第 3 步和第 4 步。
    • 是的,但是您的hookModuleRoutes 与我在上一个问题中的回答不同。
    • 另外,当您使用 hook moduleRoutes 时,无需在后端的 SEO 部分进行任何配置。
    • @abrfra Tools::getValue('ean13') 应该可以工作。至少对于 PS1.6,我已经 2 年没有接触过 prestashop,所以我不知道最新版本。
    猜你喜欢
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多