php 如何在 Zend 中重定向到不同的控制器操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16833243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:45:50  来源:igfitidea点击:

How to redirect to a different controller action in Zend

phpzend-frameworkzend-authzend-acl

提问by Sonu Jha

I am creating a simple website where users can sign up, and then sign in and add text articles. Without signing in, a visitor will have the role of a guest, and will only be able to view articles. I am doing this as an exercise in Zend framework 1, as I have just begun learning Zend. I will make a controller AuthController for login, but I want to know how do I redirect to the login action in that controller, from my indexAction in IndexController. Also, how do I make use of a custom plugin to implement this kind of access control? How do I invoke it?

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

回答by RockyFord

jalpesh and Hasina are both correct with their short answers.

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

Jalpesh's example would be a shorthand call to the action helper redirector(), which defaults to the gotoSimple()method of redirection, except he seems to have the parameters backwards.

Jalpesh 的示例是对 action helper 的速记调用redirector(),默认为gotoSimple()重定向方法,但他似乎将参数设置为向后。

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

would be more verbosely called as:

会更详细地称为:

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

There are several ways to use the Redirector action helper, this is just a very common example.

有几种方法可以使用Redirector 操作助手,这只是一个非常常见的例子。

The example provided by Hasina is a call to the controller utility method _redirect()a much more limited bit of code then the Redirectorhelper is but still very useful.

Hasina 提供的示例是对控制器实用程序方法_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());

apparently as of ZF 1.7 there is a new method not in the documentation (found this bit in the docblock) that is prefered:

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

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

Both of these utility methods are proxies for:

这两种实用方法都是代理:

 Zend_Controller_Action_Helper_Redirector::gotoUrl()

Hope this helps

希望这可以帮助

回答by Hasina

You can redirect inside an action method using:

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

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

回答by Rafael Kassner

If you are using routes, you can use $this->getHelper('Redirector')->setGotoRoute(array(), 'routeName');

如果您正在使用路线,则可以使用 $this->getHelper('Redirector')->setGotoRoute(array(), 'routeName');

回答by Jalpesh Patel

I think this will helps you

我认为这会帮助你

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

by this way you can call another controller.

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