【问题标题】:How to redirect to a different controller action in Zend如何在 Zend 中重定向到不同的控制器动作
【发布时间】:2013-05-30 10:03:23
【问题描述】:

我正在创建一个简单的网站,用户可以在其中注册,然后登录并添加文本文章。如果不登录,访问者将拥有访客角色,并且只能查看文章。我在 Zend 框架 1 中做这个练习,因为我刚刚开始学习 Zend。我将为登录创建一个控制器 AuthController,但我想知道如何从 IndexController 中的 indexAction 重定向到该控制器中的登录操作。另外,如何使用自定义插件来实现这种访问控制?如何调用它?

【问题讨论】:

    标签: php zend-framework zend-auth zend-acl


    【解决方案1】:

    jalpesh 和 Hasina 的简短回答都是正确的。

    Jalpesh 的示例是对动作助手 redirector() 的简写调用,默认为 gotoSimple() 重定向方法,但他似乎将参数向后设置。

    //corrected
    $this->_helper->redirector($action, $controller);
    

    更详细地称为:

    $this->getHelper('Redirector')->gotoSimple($action, $controller = null, $module = null, array $params = array());
    

    Redirector action helper有多种使用方式,这只是一个非常常见的例子。

    Hasina 提供的示例是对控制器 utility method _redirect() 的调用,这是一段比 Redirector 帮助器更有限的代码,但仍然非常有用。

    //only accepts a url string as the first arg
    //deprecated as of ZF 1.7 still in documentation
    $this->_redirect($url, array $options = array());
    

    显然,从 ZF 1.7 开始,有一种新方法不在文档中(在文档块中找到此位),这是首选:

    //valid as of ZF 1.7, not in documentation
    $this->redirect($url, array $options = array());
    

    这两种实用方法都是以下方面的代理:

     Zend_Controller_Action_Helper_Redirector::gotoUrl()
    

    希望对你有帮助

    【讨论】:

    • 感谢洛基福特!使用 $this->_redirect('/auth/login') 我收到此错误:在此服务器上找不到请求的 URL /demo/auth/login。我的项目文件夹是 demo,里面有文件夹 application、library 和 public。 index.php 在公共内部,我将浏览器指向 localhost/demo/public/。
    【解决方案2】:

    您可以使用以下方式在操作方法内重定向:

    $this->redirect('/module/controller/action/');
    

    【讨论】:

    • 我猜这在 ZF 的最新版本中被重命名为 $this->redirect()。
    【解决方案3】:

    如果你使用路由,可以使用$this->getHelper('Redirector')->setGotoRoute(array(), 'routeName');

    【讨论】:

      【解决方案4】:

      我认为这会对你有所帮助

      $this->_helper->redirector('controller','action');
      

      通过这种方式你可以调用另一个控制器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-17
        • 2012-07-10
        • 1970-01-01
        • 2012-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多