在prestashop中创建一个url重写模块 [英] Creating a url rewrite module in prestashop

查看:179
本文介绍了在prestashop中创建一个url重写模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建自己的



正如你所看到的模块只包含的两个文件。主模块配置文件customRoute.php和controllers / front / routeController.php中的控制器



以下两个文件的代码:


$ b $ p 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('您确定要卸载?'); (!配置:: get('customRoute'))
$ this-> warning = $ this-> l('No name provided');



$ b $ 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;

返回true;

$ b $ public function uninstall()
{
if(!parent :: uninstall()||
!Configuration :: deleteByName('customRoute ')

返回false;

返回true;


public function hookDisplayHeader()
{
$ this-> context-> controller-> addCSS($ this-> _path。' css / mymodule.css','all');


public function hookModuleRoutes($ params)
{
return [$ b $'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(); (''product_attribute','pa')
- >> select('pa.ean13 ='。(int )工具::的getValue( 'EAN13'));
$ productId = Db :: getInstance(_PS_USE_SQL_SLAVE _) - > getValue($ query);
if($ productId){
Tools :: redirect($ this-> context-> link-> getProductLink($ productId));
} else {
Tools :: redirect('pagenotfound');
}
}
}

我需要提及的是,我得到了一些帮助,因为这是我写的第一个自定义模块的代码。我想我错过了一个或多个关键项目,并希望有人能帮助我解决这些问题。



模块安装并加载到前端,所以它与安装无关,但与我使用的功能。



如果您有任何问题,请在评论部分询问他们
As总是,提前感谢!

解决方案

那么你的模块类是好的,但你的控制器不是。
$ b

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

class MyModuleNameControllerNameModuleFrontController extends ModuleFrontController



所以你的控制器应该是

class CustomRouteRouteControllerModuleFrontController extends ModuleFrontController



编辑

问题在controll中有 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'
]
]
];

解决了它并且路由工作正常。


I'm trying to build my own custom prestashop module. The module is very simple. It needs to read the url and if the url of the visitor is equal to an product code it needs to redirect the user to that specific product page. For this i'm using the following url:

www.example.com/ean13/{ean13}

So when the visitor, for example, tries to visit the page:

www.example.com/ean13/1121312341

a query has to start running and has to search for that 'ean13' product code. If the product code exists the user needs to be redirected to the specific products page.

So i've already build the basics of my module and currently the setup is the image below:

As you can see the module only consists of two files. The main module configuration file "customRoute.php" and a controller in "controllers/front/routeController.php"

The code of both files below:

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');
        }
    }
}

I need to mention that i did obtain this code with some help since this is the first custom module i'm writing. I think i'm missing a, or multiple key items and hope that someone could help me out with those things.

The module is installed and loaded in the front so it has nothing to do with the install but with the functions build up i'm using.

if you have any questions please ask them in the comments section As always, thanks in advance!

解决方案

Well your module class is ok, but your controller is not.

I've answered this already before I think but module front controller class must be declared as following:

class MyModuleNameControllerNameModuleFrontController extends ModuleFrontController

so your controller should be

class CustomRouteRouteControllerModuleFrontController extends ModuleFrontController

Edit

Issue is having Controller in controller name and controller filename.

Renaming routeController.php to route.php, CustomRouteRouteControllerModuleFrontController to CustomRouteRouteModuleFrontController and changing hookModuleRoutes to

return [
    'customroute-route-root' => [
        'rule' => 'ean13/{:ean13}.html',
        'controller' => 'route',
        'keywords' => [
            'ean13' => ['regexp' => '[0-9]+', 'param' => 'ean13']
        ],
        'params' => [
            'fc' => 'module',
            'module' => 'customroute'
        ]
    ]
];

solves it and routing works properly.

这篇关于在prestashop中创建一个url重写模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆