Zend Framework: how to redirect to source URL after login?

I am trying to implement a login system that will be smart enough to redirect the user to the page they were on before they (or were forced to) go to the login page.

I know this seems like a similar question to this , and this one , but they do not address both of my scripts.

There are two scenarios here:

  • The user specifically decides to go to the login page:

    <a href="<?php echo $this->url(array(
        'controller'=>'auth',
        'action'=>'login'), 'default', true); ?>">Log In</a>
    
  • The user is redirected as he tries to access protected content:

    if (!Zend_Auth::getInstance()->hasIdentity()) {
        $this->_helper->redirector('login', 'auth');
    }
    

How can I implement a solution for this without displaying a "redirect to" URL in the address bar?

+3
1

URL . , - . . , , URL- .

:

class Your_Application_Plugin_Access extends Zend_Controller_Plugin_Abstract {
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        foreach (self::current_roles() as $role) {
            if (
                Zend_Registry::get('bootstrap')->siteacl->is_allowed(
                    $role,
                    new Site_Action_UriPath($request->getPathInfo())
                )
            ) return; // Allowed
        }

        $this->not_allowed($request);
    }

    private function not_allowed(Zend_Controller_Request_Abstract $request) {
        $destination_url = $request->getPathInfo();

        // If the user is authenticted, but the page is denied for his role, show 403
        // else,
        // save $destination_url to session
        // redirect to login page, with $destination_url saved:
        $request
            ->setPathInfo('/login')
            ->setModuleName('default')
            ->setControllerName('login')
            ->setActionName('index')
            ->setDispatched(false);
    }

    ...

}

current_roles() "", , Zend_Auth::hasIdentity() false.

+3

Source: https://habr.com/ru/post/1726959/


All Articles