Creating a url rewrite module in prestashop

倖福魔咒の 提交于 2019-12-24 20:32:39

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/49430883/creating-a-url-rewrite-module-in-prestashop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!