简体   繁体   中英

Zend Framework redirect in front controller plugin causes redirect loop

class Ef_AppSecurity extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        if (!Zend_Auth::getInstance()->getIdentity())
        {
            $redirect = new Zend_Controller_Action_Helper_Redirector();
            $redirect->gotoSimpleAndExit('login', 'auth');
        }
    }
}

It redirects and changes to the new url however in the browser it creates a redirect loop. I'm wondering if the problem could stem from the apache mod_rewrite settings.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Do not append that plugin on login/auth, or ; extend the criteria to

if (!Zend_Auth::getInstance()->getIdentity()
     && $this->getRequest()->getControllerName() != 'login'
     && $this->getRequest()->getActionName() != 'auth')

You must add a exception for the login page. Otherwise that will be redirected as well back on to itself causing the loop.

So if your login page is in the controller called 'login' and 'index' action you need to add a exception for that page and any other page with might handle the form.

    if (!Zend_Auth::getInstance()->getIdentity() 
        && $this->getRequest()->getControllerName() != 'login'
        && $this->getRequest()->getActionName() != 'index')
        )
    {
        $redirect = new Zend_Controller_Action_Helper_Redirector();
        $redirect->gotoSimpleAndExit('login', 'index');
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM