Magento 1

Requiring Cookies For a Controller Action in Magento

Magento provides a convenient mechanism for the detection and redirection of the user if they’re not enabled. Magento’s preDispatch() method in Mage_Core_Controller_Varien_Action is responsible for this functionality where the current action name is compared against the _cookieCheckActions and the user is redirected if necessary.

To add particular actions to the cookie check, simply add them to the _cookieCheckActions array in the controller’s preDispatch method:

    public function preDispatch()
        $this->_cookieCheckActions[] = 'index';
        $this->_cookieCheckActions[] = 'guest';


And voilà, the user will be redirected to the enable cookies page!

Magento 1

Redirecting to the Login Page for a Given Route

The following code sample will redirect from any routename that isn’t ‘customer’. More exceptions could be added as needed to add a white or black list for pages.



The Observer

class Rayware_Core_Model_Loginobserver{
	// Prevent the user from going to any route that doesn't start with customer
	public function checkLoggedIn($observer){
		 $controllerAction = $observer->getEvent()->getControllerAction();
		 $request = $controllerAction->getRequest();
		 $session = Mage::getSingleton('customer/session');
		 $route = $request->getRouteName();
		 $action = $request->getControllerName();

		 if ($session->isLoggedIn() || $route == 'customer') {
         $session->setAfterAuthUrl( Mage::helper('core/url')->getCurrentUrl() );
         $session->setBeforeAuthUrl( Mage::helper('core/url')->getCurrentUrl() );