Home Creating a url rewrite module in prestashop
Reply: 0

Creating a url rewrite module in prestashop

user4311
1#
user4311 Published in July 23, 2018, 9:57 am

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!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.358879 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO