boruch
boruch

Reputation: 463

Event dispatch in Magento before redirect

I'm running into an issue when using event dispatch in Magento.

I'm using controller_action_predispatch to set a frontend session variable from a parameter in the URL.

Now, the issue seems like, when the user comes to the site initially, they might lend on a page that will redirect them to base URL (such a example.com to www.example.com). But for some reason, after redirect, the session variable is lost...

Any ideas?

Thank you.

EDIT:

adding the code used:

public function grabRef($observer) {
    $ref = Mage::app()->getRequest()->getParam('ref', $default);
    if (isset($ref) && !is_null($ref) and !empty($ref)) {

        Mage::getSingleton('core/session',array('name'=>'frontend'))->setRefid($ref);

    }
}

Upvotes: 3

Views: 2437

Answers (1)

benmarks
benmarks

Reputation: 23205

There are only two remotely useful events dispatched prior to this redirection, but they are not specific to the redirect:

  • controller_front_init_before
  • controller_front_init_routers

The redirect depends on the "Auto-redirect to Base URL" setting from System > Configuration > Web > Url Options, which is evaluated by Mage_Core_Controller_Varien_Front->_checkBaseUrl(). This redirect occurs before any dispatching takes place, and it does not append GET or POST data, hence the loss of the param you are trying to capture.

Normally sessions are initialized under adminhtml or frontend session namespace based on the controller class being used (ref the action controller superclass method Mage_Core_Controller_Varien_Action->preDispatch(). You should be able to move your observer configuration under global/events/controller_front_init_before. Note that you must do this in the global event area, as the frontend event configuration part does not load until after this event is dispatched. That particular scenario cost me an hour once!

Upvotes: 6

Related Questions