php Zend框架中重定向和转发有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2551238/
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
What is the difference between redirect and forward in Zend framework
提问by user1400
What is the difference between redirect and forward in Zend framework?
Zend 框架中的重定向和转发有什么区别?
When we should use redirect and when should we use forward?
什么时候应该使用重定向,什么时候应该使用转发?
回答by user187291
Imagine you get a phone call in the office. Someone wants to talk to sales. If you say "please call 123456" and hang up, this is redirect. If you say "wait a minute" and just transfer the call to them, this is forward. ;)
想象一下,你在办公室接到一个电话。有人想和销售人员谈谈。如果您说“请拨打 123456”并挂断,这是重定向。如果您说“稍等”,然后将呼叫转接给他们,这就是forward。;)
回答by Gordon
_forward()just forwardseverything to another controller action, while _redirect()sends a header, meaning you create a new HTTP Request and go through the entire dispatch process with it.
_forward()只是将所有内容转发到另一个控制器操作,同时_redirect()发送一个标头,这意味着您创建一个新的 HTTP 请求并使用它完成整个调度过程。
For instance, if you call up http://example.com/foo/baryou'd call the foocontroller and baraction. If you forward inside the baraction to the bazaction, e.g. within the very same request, the browser would still be on the same URL, while when doing a redirect, ZF would instruct the browser to load http://example.com/foo/baz.
例如,如果您调用http://example.com/foo/bar,您将调用foo控制器和bar操作。如果你在bar动作内部转发到baz动作,例如在同一个请求中,浏览器仍然会在同一个 URL 上,而在进行重定向时,ZF 会指示浏览器加载http://example.com/foo/巴兹。
Essentially, _forward()does
从本质上讲,_forward()确实
$request->setActionName($action)
->setDispatched(false);
while _redirect()does
而_redirect()不是
$this->_helper->redirector->gotoUrl($url, $options);
I usually do redirects when I want to prevent reload a page resulting in reposting form data.
当我想防止重新加载页面导致重新发布表单数据时,我通常会进行重定向。
See these:
看到这些:
回答by pinkgothic
You would use _forward()for cases where you want the URL to stay the same - though beware, it does mean whatever base controller class you're using is called twice.
对于希望 URL 保持不变的情况,您可以使用_forward()- 尽管要小心,但这确实意味着您使用的任何基本控制器类都会被调用两次。
That may seem obvious or trivial, but if not kept in mind, can really screw up your application design, given that intuitive understanding of the flow is that one request calls one controller instance. E.g. it means request-scope singletons haveto be declared as static, or _forward()will break them.
这可能看起来很明显或微不足道,但如果不牢记,可能会真正搞砸您的应用程序设计,因为对流程的直观理解是一个请求调用一个控制器实例。例如,这意味着必须将请求范围的单例声明为static,否则_forward()会破坏它们。
回答by Andy Shellam
I would guess that a redirect sends a 301/302 back to the browser with a new URL, while a forward simply "forwards" the request to a different controller action internally but keeps the URL the same so the browser doesn't know any different.
我猜想重定向会使用新 URL 将 301/302 发送回浏览器,而转发只是在内部将请求“转发”到不同的控制器操作,但保持 URL 相同,因此浏览器不知道任何不同.
回答by tawfekov
1-redirect create a new response with header() information [302 Found or 301 == Moved permanently] and its will get to the dispatch cycle once again
1-redirect 创建一个带有 header() 信息的新响应 [302 Found or 301 == Moved Permanent] 并且它将再次进入调度周期
2-forward change the execution flow to that new request without re enter the dispatch process again
2-forward 将执行流程更改为该新请求,而无需再次重新进入调度流程
回答by Rodolphe
The redirect action ends the current page process and redirects to another. All the context will change (new controller/action) as the browser receives a redirection. It connects to a new URL
重定向操作结束当前页面进程并重定向到另一个页面。当浏览器收到重定向时,所有上下文都会改变(新的控制器/动作)。它连接到一个新的 URL
Whereas the forward will stay on the same page, but will leave the context unchanged. You can see this as a function call. Your views will be loaded as usual.
而转发将保持在同一页面上,但将保持上下文不变。您可以将其视为函数调用。您的视图将照常加载。

