【问题标题】:The _redirect() function in zend frameworkzend 框架中的 _redirect() 函数
【发布时间】:2013-11-16 02:55:29
【问题描述】:

我在 library/My/Utils/Utils.php 中创建了一个文件。文件内容为:

class My_Utils_Utils{
    public function test(){
        $this->_redirect('login');   
    }
}

这个类是从布局中调用的;问题出在_redirect(); 我收到此错误:页面未正确重定向。我的问题是如何从您在 ZEND 框架 1 中创建的类中调用 _redirect() 函数。 提前致谢。

【问题讨论】:

标签: function class zend-framework redirect layout


【解决方案1】:

_redirect 函数由 Zend_Controller_Action 类提供。您可以通过两种方式解决此问题:

  1. 扩展 Zend_Controller_Action 并使用 _redirect

    class My_Utils_Utils extends Zend_Controller_Action {
       public function test(){
        $this->_redirect('login');   
       }
    

    }

在布局中:

     $request = Zend_Controller_Front::getInstance()->getRequest();
     $response = Zend_Controller_Front::getInstance()->getResponse()
     $util = new My_Utils_Utils($request, $response); // The constructor for Zend_Controller_Action required request and response params.
    $util->test();
  1. 使用 gotoUrl() 函数 Zend_Controller_Action_Helper_Redirector::gotoUrl()

     $redirector = new Zend_Controller_Action_Helper_Redirector();
     $redirector->gotoUrl('login');
    
     //in layout : 
     $util = new My_Utils_Utils();
     $util->test();
    

【讨论】:

  • 相信我已经尝试过了。事实是,如果我从控制器调用类和函数,它可以工作,但如果我从布局调用它就不行。
【解决方案2】:

您可以使用redirector action-helper,您可以使用以下方法从HelperBroker 静态获取:

// get the helper
$redirectHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');

// call methods on the helper
$redirect->gotoUrl('/some/url');

但是应该注意,布局被认为是视图层的一部分。通常,任何导致重定向的检查都应该在请求调度周期的早期进行,通常在控制器或front-controller plugin 中。

【讨论】:

  • 相信我已经尝试过了。事实是,如果我从控制器调用类和函数,它可以工作,但如果我从布局调用它就不行。任何想法为什么?
  • 请不要把 zend 的文档发给我。我是新手,我想要适当的例子,而不是理论。谢谢理解我
  • 啊,可能需要改用gotoUrlAndExit()。这实际上会触发重定向,而不仅仅是在响应对象中设置标头。
【解决方案3】:

使用redirect() 代替_redirect()。用法是:

$this->redirect(<action>, <controller>, <module>, <param>);

在您的情况下,$this-&gt;redirect('login'); 应该可以解决问题。

【讨论】:

  • 它没有 :( 。实际上它不会在 zend 中退出 redirect('') 函数,可能在 codeigniter 中。在 zend 中是 _rediect('') 带下划线。
  • @Naghi _redirect() 方法自 Zend Framework 1.7 起已弃用。改用redirect()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多