重定向到zend框架中的上一页

Mor*_*ani 29 url session redirect zend-framework

我想在登录页面中为我的登录表单操作(作为查询)添加重定向URL,因此在登录后,可以访问他或她正在浏览的上一页.

首先,我考虑使用Zend Session并将每个页面的url保存在变量中.但我在文档中读到它有开销.那么,有更好的方法吗?或者是否有其他方式使用zend会话而没有开销?

Mos*_*tov 17

首先,您需要获取重定向的原始URL.您可以通过Zend_Controller_Request类来执行此操作:

$url = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
Run Code Online (Sandbox Code Playgroud)

或者简单地通过:

$url = $_SERVER['REQUEST_URI'];
Run Code Online (Sandbox Code Playgroud)

然后,棘手的部分是通过用户请求传递它.我建议使用库Zend_Session,尽管使用POST参数也是合法的:

$session = new Zend_Session_Namespace('Your-Namespace');
$session->redirect = $_SERVER['REQUEST_URI'];
Run Code Online (Sandbox Code Playgroud)

请注意,我们保留的地址包括基本路径.要在控制器类中重定向客户端,请禁用"prependBase"选项以丢失基本路径插入:

$this->_redirect($url, array('prependBase' => false));
Run Code Online (Sandbox Code Playgroud)

  • 还可以序列化请求对象:`$ session-> redirect = serialize($ this-> getRequest())`.实际上这是我现在使用的解决方案,一年后;) (3认同)

Jes*_*sta 9

我发现作为一种简单的方法来实现这一点只是在登录表单中有一个隐藏字段.

现在,我不确定您的登录表单是否是通用HTML元素,或者实际上是一个实例Zend_Form,但如果它是一个实例Zend_Form,您可以简单地添加以下内容:

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

然后在我的身份验证脚本中,就像上面的注释一样,我有一个简单的重定向,它使用传入的值将它们返回到同一页面.

$this->_redirect($this->_request->getPost('return'));
Run Code Online (Sandbox Code Playgroud)

显然,在这两个例子中,它们只是为了压缩代码而编写的,可能并不代表实现它的最佳方法.getRequest()在我的代码中使用的两种方法实际上并没有嵌入到redirect或者中addElement,但是出于示例目的,我只是将它们放在那里.

上面的答案显然也会起作用,除非你有一些大规模的页面重定向.我现在以这种方式运行的主要原因是因为并非我的所有表单都在运行,Zend_Form并且还可以更改隐藏return输入文本框的值以进行测试.


gna*_*arf 6

基本上和Jesta在他的答案中所做的一样,但是我将以下函数添加到我的"MW_Form"类 - 这是我所有形式的超类 - 很容易$form->trackReferrer($this->getRequest());从任何形式的控制器.getReferrer()函数采用"默认"参数(如果用户禁用了REFERER标头,或者没有引用者 - 您希望将默认位置重定向回去)

  /**
   * Adds a form element named "referrer" and sets its default value to either
   * the 'referrer' param from the request, or the HTTP_REFERER header.
   *
   * @param Zend_Controller_Request_Abstract $request 
   * @return MW_Form
   * @author Corey Frang
   */
  public function trackReferrer(Zend_Controller_Request_Abstract $request)
  {
    $this->addElement('hidden', 'referrer');
    $this->setDefault('referrer', 
      $request->getParam('referrer', 
        $request->getServer('HTTP_REFERER')));
        // HTTP_REFERER not HTTP_REFERRER - grrr HTTP spec misspellings

    // use no decorator for the actual form element
    $this->referrer->setDecorators(array()); 

    // use our custom "referrer" decorator to stick the hidden before the <dl>
    $decorators = $this->getDecorators();
    $this->clearDecorators();
    foreach ($decorators as $class=>$decorator)
    {
      if (substr($class,-5) == '_Form') {
        $this->addDecorator('Referrer');
        $added = true;
      }
      $this->addDecorator($decorator);
    }
    if (!$added) $this->addDecorator('Referrer');

    return $this;
  }

  /**
   * Returns the referrer field if it exists.
   *
   * @return string | false
   * @param mixed $default The value to return if referrer isn't set
   * @author Corey Frang
   **/
  public function getReferrer($default = false)
  {
    if (!isset($this->referrer)) return $default;
    $val = $this->referrer->getValue();
    if ($val) return $val;
    return $default;
  }
Run Code Online (Sandbox Code Playgroud)

使用的装饰器 - 为您提供了不使用<dl>zend_form创建的任何行的好处:

class MW_Form_Decorator_Referrer extends Zend_Form_Decorator_Abstract  {
  /**
   * Attaches the standard "ViewHelper" decorator for the 'referrer' element
   * prepended on the content
   *
   * @return void
   * @author Corey Frang
   **/
  public function render($content)
  {
    $form = $this->getElement();
    if ($form instanceOf MW_Form)
    {
      $referrer = $form->referrer;
      if ($referrer)
      {
        $decorator = new Zend_Form_Decorator_ViewHelper(array('placement'=>self::PREPEND));
        $decorator->setElement($referrer);
        return $decorator->render($content);
      }
    }
    return "Error - No Referrer Found".$content;
  }
}
Run Code Online (Sandbox Code Playgroud)

用法示例(来自控制器):

$form = $description->getEditForm();
$form->trackReferrer($this->_request);
if ($this->_request->isPost())
{
  if ($form->process($this->_request->getPost()))
  {
    return $this->_redirect($form->getReferrer('/page'));
  }
}
Run Code Online (Sandbox Code Playgroud)