PHP是否具有Java的RequestDispatcher.forward的等价物? [英] Does PHP Have an Equivalent of Java's RequestDispatcher.forward?
问题描述
在Java中,我可以编写一个非常基本的JSP index.jsp
,如下所示:
In Java I can write a really basic JSP index.jsp
like so:
<%request.getRequestDispatcher(/ home.action)。forward(request,response); %>
这样做的结果是用户请求 index.jsp
(或只是包含目录,假设 index.jsp
是目录的默认文档)将看到 home.action
浏览器重定向,即[forward]( http://java.sun.com/javaee/5/docs/api/javax/servlet/RequestDispatcher.html#forward(javax.servlet.ServletRequest,% 20javax.servlet.ServletResponse))发生在服务器端。
The effect of this is that a user requesting index.jsp
(or just the containing directory assuming index.jsp
is a default document for the directory) will see home.action
without a browser redirect, i.e. the [forward](http://java.sun.com/javaee/5/docs/api/javax/servlet/RequestDispatcher.html#forward(javax.servlet.ServletRequest,%20javax.servlet.ServletResponse)) happens on the server side.
我可以用PHP做类似的事吗?我怀疑可以配置Apache来处理这种情况,但由于我可能无法访问相关的Apache配置,我会对依赖于PHP的解决方案感兴趣。
Can I do something similar with PHP? I suspect it's possible to configure Apache to handle this case, but since I might not have access to the relevant Apache configuration I'd be interested in a solution that relies on PHP alone.
推荐答案
如果您担心CURL可用性,那么您可以使用 file_get_contents()
和流。设置如下函数:
If you are concerned about CURL availability then you could use file_get_contents()
and streams. Setting up a function like:
function forward($location, $vars = array())
{
$file ='http://'.$_SERVER['HTTP_HOST']
.substr($_SERVER['REQUEST_URI'],0,strrpos($_SERVER['REQUEST_URI'], '/')+1)
.$location;
if(!empty($vars))
{
$file .="?".http_build_query($vars);
}
$response = file_get_contents($file);
echo $response;
}
这只是设置一个GET,但你可以用<$做一个帖子c $ c> file_get_contents()。
This just sets up a GET, but you can do a post with file_get_contents()
as well.
这篇关于PHP是否具有Java的RequestDispatcher.forward的等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!