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?
2 Answers 2
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– sergio2014年02月28日 08:14:17 +00:00Commented Feb 28, 2014 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– Marius2014年02月28日 08:15:05 +00:00Commented Feb 28, 2014 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– Marius2014年02月28日 08:19:08 +00:00Commented Feb 28, 2014 at 8:19
-
Hm, probably I did not understand something, but I copy code, and it also reload infinitely(magento is clear(new))sergio– sergio2014年02月28日 08:37:42 +00:00Commented Feb 28, 2014 at 8:37
-
@sergio Clear the cache. And make sure you copy the full method. I changed the redirect page from
customer/accounttocustomer/account/login.Marius– Marius2014年02月28日 08:48:21 +00:00Commented Feb 28, 2014 at 8:48
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-dispatchflag. 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'))
Explore related questions
See similar questions with these tags.