Does PHP Have an Equivalent of Java's RequestDispatcher.forward?

后端 未结 7 2243
南笙
南笙 2021-02-06 03:32

In Java I can write a really basic JSP index.jsp like so:

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

7条回答
  •  天命终不由人
    2021-02-06 04:15

    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;
    }
    

    This just sets up a GET, but you can do a post with file_get_contents() as well.

提交回复
热议问题