PHP是否具有Java的RequestDispatcher.forward等价物?

Wal*_*sby 9 php jsp forwarding http

在Java中,我可以编写一个非常基本的JSP,index.jsp如下所示:

<% request.getRequestDispatcher("/home.action").forward(request, response); %>

这样做的结果是请求index.jsp(或只是包含目录假设index.jsp是目录的默认文档)的用户将看到home.action没有浏览器重定向,即[转发](http://java.sun.com/javaee/5 /docs/api/javax/servlet/RequestDispatcher.html#forward(javax.servlet.ServletRequest,%20javax.servlet.ServletResponse))发生在服务器端.

我可以用PHP做类似的事吗?我怀疑可以配置Apache来处理这种情况,但由于我可能无法访问相关的Apache配置,所以我会对依赖于PHP的解决方案感兴趣.

nul*_*ull 0

如果您担心 CURL 的可用性,那么您可以使用file_get_contents()and Streams。设置如下功能:

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;
}
Run Code Online (Sandbox Code Playgroud)

file_get_contents()这只是设置了一个 GET,但你也可以用它来发帖。