简体   繁体   中英

How do I avoid redirecting 404's to the login form in a Zend Framework 1 controller plugin?

I made a controller plugin to handle authentication. If a user tries to access a page without being logged in, it saves the route of the page he was trying to access, forwards to the login page, and then when the user logs in, it redirects him to where he was trying to go.

But if the user tries to access a nonexistent page while logged out, then it still forwards to the sign-in form, but when the user signs in, it brings up an error.

How do I bring up a 404 error before the user signs in? I think I need to detect whether the route is valid within dispatchLoopStartup(). How do I do that? Or is there some other way of doing this?

class Chronos_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $request->setParam('userName', $auth->getIdentity());
        } else {
            $request->setParam('origModule', $request->getModuleName())
                    ->setParam('origController', $request->getControllerName())
                    ->setParam('origAction', $request->getActionName())
                    ->setModuleName('default')
                    ->setControllerName('sign')
                    ->setActionName('in');
        }
    }
}

Try something like this:

public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
    $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
    $auth = Zend_Auth::getInstance();
    if ($auth->hasIdentity()) {
        $request->setParam('userName', $auth->getIdentity());
    } else if ($dispatcher->isDispatchable($request)) {
        $request->setParam('origModule', $request->getModuleName())
                ->setParam('origController', $request->getControllerName())
                ->setParam('origAction', $request->getActionName())
                ->setModuleName('default')
                ->setControllerName('sign')
                ->setActionName('in');
        }
    }

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