duanlu8613 2018-03-22 14:12
浏览 96
已采纳

在prestashop中创建url重写模块

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:

Module setup

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!

  • 写回答

1条回答 默认 最新

  • doulan2827 2018-03-22 16:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?