Redirect to previous page in zend framework

2019-01-13 04:29发布

I want to add a redirection URL to my login forms action (as a query) in login page, so after loging-in, one can visit the previous page he or she was surfing.

First I thought about using Zend Session and save the url of each page in a variable. but I read in the documentation that it has overhead. So, is there a better way to do so? or is there an other way to use zend session with no overhead?

11条回答
贼婆χ
2楼-- · 2019-01-13 04:59

In addition to gnarfs answer, i modified it to making it validate - for those of you that get a kick of it.

$this->addDecorator(array('WrapClose' => 'HtmlTag'), array('tag' => 'div', 'placement' => 'PREPEND', 'closeOnly' => true));
$this->addDecorator('Referrer'); 
$this->addDecorator(array('WrapOpen' => 'HtmlTag'), array('tag' => 'div', 'placement' => 'PREPEND', 'openOnly' => true));
查看更多
小情绪 Triste *
3楼-- · 2019-01-13 04:59

$this->_redirect($this->getRequest()->getServer('HTTP_REFERER'));

查看更多
\"骚年 ilove
4楼-- · 2019-01-13 05:03

What I have found as a simple method to accomplish this is just to have a hidden field in your login form.

Now, im not sure if your login form is a generic HTML element or is actually an instance of Zend_Form, but if its an instance of Zend_Form, you could simple add the following:

$this->addElement('hidden', 'return', array(
        'value' => Zend_Controller_Front::getInstance()->getRequest()->getRequestUri(),             
            ));

Then in my authentication script, like the comment above, I have a simple redirect that uses the value passed in to return them to the same page.

$this->_redirect($this->_request->getPost('return'));

Obviously in these two examples, they are just written to compact the code and probably do not represent the best way to accomplish it. The two methods using the getRequest() in my code are actually not embedded in the redirect or the addElement, but for example purposes I just slid them in there.

The answer above will obviously work as well, unless you have some massive page redirection going on. The main reason I am running mine this way right now is because not all of my forms are running in Zend_Form and its also nice being able to change the value of the hidden return input text box for testing purposes.

查看更多
Ridiculous、
5楼-- · 2019-01-13 05:03

If you are not a fan of passing variables via session you could try to get $_SERVER['HTTP_REFERER'] variable in a safe way. Basically it checks if your referrer url and matches with your server local name, and scheme (http/https).

class My_Tools
{   
    public static function doesUrlMatchServerHttpHost($url)
    {       
        $scheme = Zend_Controller_Front::getInstance()->getRequest()->getScheme();
        $httpHost = Zend_Controller_Front::getInstance()->getRequest()->getHttpHost();
        $needleUrl = $scheme.'://'.$httpHost.'/';
        if (strpos($url, $needleUrl) !== 0)
        {
            return false;
        }
        return true;
    }

    public static function safelyGetReferrerUrl($default)
    {
        if ( isset($_SERVER['HTTP_REFERER']) == false){
            return $default;
        }
        if (self::doesUrlMatchServerHttpHost($_SERVER['HTTP_REFERER']) == false){
            return $default;
        }
        return $_SERVER['HTTP_REFERER'];
    }
}

And then just

$referrerUrl = My_Tools::safelyGetReferrerUrl('/');

As default you can set local uri ('/')

查看更多
欢心
6楼-- · 2019-01-13 05:04

I'm sure there is some built in method for doing this somewhere in ZF, but I'm lazy, so I did it this way:

Create your own App_Controller_Action class (create /library/App/Controller/Action.php). Extend all of your controllers off of this class

In each of my controllers, I call $this->_initAuth(), function pasted below:

protected function _initAuth()
{
    $auth = Zend_Auth::getInstance();
    if (!$auth->hasIdentity() && strcmp($_SERVER['REQUEST_URI'], '/auth/login') <> 0)
        $this->_redirect('/auth/login' . $_SERVER['REQUEST_URI']);
    else
        $this->_identity = $auth->getIdentity();
}

In my AuthController, I do the following to make sure my form points to the full url:

$uri = str_replace('/auth/login','',$_SERVER['REQUEST_URI']);
if (strlen($uri) > 0)
    $form->setAction($this->_helper->url('login') . $uri);
else
    $form->setAction($this->_helper->url('login'));

If the login validates, then I do the following:

if (strlen($uri) > 0)
    $this->_redirect($uri);
else
    $this->_redirect('/');
查看更多
Luminary・发光体
7楼-- · 2019-01-13 05:07

First, you need to grab the original url for the redirection. You can do that by the Zend_Controller_Request class via:

$url = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();

or simply by:

$url = $_SERVER['REQUEST_URI'];

Then, the tricky part is to pass it through the user request. I recommend to use the library Zend_Session, despite using a POST parameter is also legitimate:

$session = new Zend_Session_Namespace('Your-Namespace');
$session->redirect = $_SERVER['REQUEST_URI'];

Please note that the address we kept includes the base path. To redirect the client in the controller class, disable the option 'prependBase' to lose the base path insertion:

$this->_redirect($url, array('prependBase' => false));
查看更多
登录 后发表回答