Magento Event On Any Page Load

Magento Event On Any Page Load

August 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Event On Any Page Load

Magento: Firing an Event on Every Page Load

In Magento, firing an event on every page load can be quite useful for implementing global business logic or adding HTML elements like the canonical tag to all pages without depending on user sessions.

The core_controller_response_redirect Event

The core event we are looking for is called core_controller_response_redirect. This event is fired right before Magento redirects to another page, which happens after every significant action like placing an order or adding a product to the cart. However, it doesn’t cover simple page loads.

The controller_action_predispatch Event

To catch all page loads, including static pages and CMS pages, we need to use the controller_action_predispatch event. This event is triggered before any controller action is executed, making it ideal for our purpose.

public function aroundDispatch(\Magento\Framework\App\ActionInterface $subject, \Closure $proceed)
{
    $eventManager = $this->eventManager;
    $request = $subject->getRequest();
    $response = $subject->getResponse();
    $actionName = $request->getRouteName() . '_' . $request->getControllerName() . '_' . $request->getActionName();

    try {
        $proceed();
    } catch (\Exception $e) {
        $this->log($e);
    }

    $eventManager->dispatch('controller_action_predispatch_' . $actionName, [
        'controller_action' => $subject,
        'request' => $request
    ]);

    // Your custom logic here

    return $response;
}

In the example above, we use the aroundDispatch method to intercept the controller’s dispatch process. After the action is executed (or if an exception was caught), we fire our own event using $eventManager->dispatch().

Adding Custom Logic

Within your extension or module, you can now add your business logic inside this event observer.

For example, to add a canonical tag to all pages:

public function execute(\Magento\Framework\EventInterface $event)
{
    $request = $event->getData('request');
    if ($request->isDispatched()) {
        $response = $event->getData('controller_action')->getResponse();
        $canonicalUrl = $this->_storeManager->getStore()->getBaseUrl() . $request->getPathInfo();
        $response->setHeader('Link', '<' . $canonicalUrl . ' rel="canonical"');
    }
}

This code adds the canonical tag to the HTTP response header for all pages. The isDispatched() check ensures we only process page loads and not redirect actions.

Conclusion

The controller_action_predispatch event in Magento provides a powerful way to execute global logic on every page load, regardless of whether the page is dynamic or static content. This event allows us to add functionality like canonical tags without relying on user sessions or database queries.

Remember that adding too many observers can slow down your site’s performance, so always optimize your code and use proper logging techniques.

For more advanced Magento development topics, I recommend checking out our comprehensive guides at Laravel Company.

Checkout Catalog Cache Security

Our network