3

So, I need to redirect user to some page(tt can be CMS or customer/account) if he did not logged. And on this page(CMS or customer/account) I will have a form to login/register. So, I made an observer for controller_action_predispatch event and method which will be implemented it, I did:

public function redirectNotLogged(Varien_Event_Observer $observer)
    {
        if(! Mage::helper('customer')->isLoggedIn()){
            Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account'));
        }
    }

But I have a problem, I cannot get to this page, because it infinitely redirect me. Any advices?

10

You are getting the infinite loop, because when you redirect to Mage::getUrl('customer/account') your observer is called again and redirects you to the same page.
You need to add a rule for when the redirect is not made.
Something like this.

public function redirectNotLogged(Varien_Event_Observer $observer)
{
    $action = strtolower(Mage::app()->getRequest()->getActionName());
    $controller = strtolower(Mage::app()->getRequest()->getControllerName());
    $openActions = array(
        'create',
        'createpost',
        'login',
        'loginpost',
        'logoutsuccess',
        'forgotpassword',
        'forgotpasswordpost',
        'resetpassword',
        'resetpasswordpost',
        'confirm',
        'confirmation'
    );
    if ($controller == 'account' && in_array($action, $openActions)) {
         return $this; //if in allowed actions do nothing.
    }
    if(! Mage::helper('customer')->isLoggedIn()){
        Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account/login'));
    }
}
  • Did, but it also redirect infinitely.. – sergio Feb 28 '14 at 8:14
  • @sergio. Like I said, the code is not tested. Is just an idea. You can debug the code above to see why you get the second redirect. – Marius Feb 28 '14 at 8:15
  • @sergio. Ok I tested the code. There was an error in my method. I fixed it now. See my answer update. It works fine for me. – Marius Feb 28 '14 at 8:19
  • Hm, probably I did not understand something, but I copy code, and it also reload infinitely(magento is clear(new)) – sergio Feb 28 '14 at 8:37
  • @sergio Clear the cache. And make sure you copy the full method. I changed the redirect page from customer/account to customer/account/login. – Marius Feb 28 '14 at 8:48
3

An improved solution:

  • redirect for every route except "customer". This will not interfer with login, create account, password reset... and other customer actions already redirect to the login anyway.
  • set the no-dispatch flag. This is important because otherwise the action is executed and the page rendered even though it will not be shown to the user
  • use Mage_Customer_Model_Session::authenticate() to check the login and redirect. This will also save the currently requested URL in the session and redirect back after login

Code:

public function redirectNotLogged(Varien_Event_Observer $observer)
{
    $controller = $observer->getControllerAction();
    if ($controller->getRouteName() === 'customer') {
        return;
    }
    if (! Mage::getSingleton('customer/session')->authenticate($controller)) {
        $controller->setFlag('', 'no-dispatch', true);
    }
}

If you want to use a custom login page instead of customer/account/login, you can pass this custom URL as second parameter to authenticate(), but you also have to check for this action additionally to not get an infinite redirect loop again:

if ($controller->getFullActionName() === 'your_custom_page') {
    return;
}

and

authenticate($controller, Mage::getUrl('your/custom/page'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.