問題描述
Zend 框架中重定向和轉發的區別是什么?
What is the difference between redirect and forward in Zend framework?
什么時候應該使用重定向,什么時候應該使用轉發?
When we should use redirect and when should we use forward?
推薦答案
_forward()
只是將所有內容轉發到另一個控制器操作,而 _redirect()
發送一個標頭,這意味著您創建一個新的 HTTP 請求并使用它完成整個調度過程.
_forward()
just forwards everything 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.
例如,如果您調用 http://example.com/foo/bar 你會調用 foo
控制器和 bar
動作.如果您在 bar
操作內部轉發到 baz
操作,例如在同一個請求中,瀏覽器仍然在同一個 URL 上,而在進行重定向時,ZF 會指示瀏覽器加載 http://example.com/foo/baz.
For instance, if you call up http://example.com/foo/bar you'd call the foo
controller and bar
action. If you forward inside the bar
action to the baz
action, 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.
本質上,_forward()
確實
$request->setActionName($action)
->setDispatched(false);
而 _redirect()
確實
$this->_helper->redirector->gotoUrl($url, $options);
當我想防止重新加載頁面導致重新發布表單數據時,我通常會進行重定向.
I usually do redirects when I want to prevent reload a page resulting in reposting form data.
看到這些:
- Zend 框架,$this->_forward 正在做什么
- http://osdir.com/ml/php.zend.framework.mvc/2007-09/msg00038.html
- http:///framework.zend.com/svn/framework/standard/trunk/library/Zend/Controller/Action/Helper/Redirector.php
這篇關于Zend框架中重定向和轉發有什么區別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!