Hammerbot
Hammerbot

Reputation: 16364

Prestashop beforeRequest Middleware

I am trying to build a module for Prestashop 1.6 that would redirect the user if the targeted URL is present in a database.

What I'm going to do is the following:

public function checkRedirection ($url) {
    $line = Db::getInstance()->executeS('SELECT * FROM ps_custom_redirection WHERE url = ' . pSQL($url));

    if (!sizeof($line)) {
        return null;
    }

    header('Location: ' . $line[0]['destination']);
    http_response_code($line[0]['http_code']);
    exit();
}

Now, I could run this function when the displayTop hook is fired. But I would rather launch this function at the beginning of the request's process.

Does Prestashop provide such a hook? If not, can I create one? Where should I write the code to fire it?

Upvotes: 1

Views: 415

Answers (1)

Indrė
Indrė

Reputation: 1136

The fist hook executed is actionDispatcher – you can use it if you want.

You'll find this hook executed in /classes/Dispatcher.php. Search for the code Hook::exec('actionDispatcher', $params_hook_action_dispatcher);.

If you want to add this hook to your module, you need to use its name in the main module file like this:

public function install() { return parent::install() && $this->registerHook('actionDispatcher'); }

public function hookActionDispatcher($params) { // your code Tools::redirect($url); }

In Prestashop Tools::redirect($url); is used if redirecting.

Upvotes: 1

Related Questions