在Phalcon中重定向和分派之间有什么区别? [英] what's the difference between redirect and dispatch in phalcon?

查看:64
本文介绍了在Phalcon中重定向和分派之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想从另一个页面切换到页面时,可以同时找到$this->dispatcher->forward()$this->response->redirect().两者似乎都可以.

When I want to change to a page from another, I can find both $this->dispatcher->forward() and $this->response->redirect(). Both seem to work ok.

它们之间有什么区别,何时应该在另一个上使用?

What's the difference between them, and when should I use one over the other?

推荐答案

广义上讲,Redirect将执行http重定向(带有标头位置).这意味着客户端的浏览器将更改页面.它会处理到新的路由(也可以用于转到另一个网站),并且实际脚本将结束.

Broadly speaking, Redirect will do an http redirection (with the header location). It means that the browser of the client will change the page. It processes to a new routing (it can also be used to go to another website) and the actual script will end.

Forward是内部的,客户端的浏览器看不到任何区别,您只需执行一个不同的控制器即可.调度循环使用户可以将执行流转发到另一个控制器/动作.这对于检查用户是否可以访问某些选项,将用户重定向到其他屏幕或简单地重用代码非常有用.但是,请记住,进行转发"与进行HTTP重定向是不同的.尽管他们显然得到了相同的结果. 转发"不会重新加载当前页面,所有重定向都在单个请求中进行,而HTTP重定向则需要两个请求才能完成该过程.

Whereas Forward is internal, the browser of the client wont see any difference, you just execute a different controller.The dispatch loop allows the user to forward the execution flow to another controller/action. This is very useful to check if the user can access to certain options, redirect users to other screens or simply reuse code. But, Keep in mind that making a "forward" is not the same as making an HTTP redirect. Although they apparently got the same result. The "forward" doesn’t reload the current page, all the redirection occurs in a single request, while the HTTP redirect needs two requests to complete the process.

类似的类比也可以在JSP中显示,

Similar analogy can be shown in JSP as well,

HttpServletResponsesendRedirect(String path)方法将告诉客户端应将请求发送到指定的路径.因此,客户端将建立一个新请求并将其提交给服务器.客户的历史记录将被更新,因此前进和后退按钮将起作用.此方法对于重定向到其他服务器和域上的页面很有用.

The sendRedirect(String path) method of HttpServletResponse will tell the client that it should send a request to the specified path. So the client will build a new request and submit it to the server. The client's history will be updated so the forward and back buttons will work. This method is useful for redirecting to pages on other servers and domains.

RequestDispatcher的forward方法会将转发的ServletRequestServletResponse转发到getRequestDispatcher(String path).中指​​定的路径.响应不会发送回客户端,因此客户端不会了解服务器上资源的这种更改.此方法对于服务器资源之间(servlet到servlet)之间的通信很有用.此方法比使用sendRedirect更快,因为不需要网络往返服务器和服务器.

The forward method of RequestDispatcher will forward the ServletRequest and ServletResponse that it is passed to the path that was specified in getRequestDispatcher(String path). The response will not be sent back to the client and so the client will not know about this change of resource on the server. This method is useful for communicating between server resources, (servlet to servlet). This method is faster than using sendRedirect as no network round trip to the server and back is required.

这篇关于在Phalcon中重定向和分派之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆