What is equivalent of RequestDispatcher#forward() Of Java in PHP?
RequestDispatcher basically dispatches a request to some other resource i.e it doesn't do a redirect but simply forwards the page to some page(resource). Thus browser's history etc is not updated and the parameters that开发者_如何学Go are available in request
are still available on another page since we are not creating a new request using redirect.
What is equivalent of this in PHP? And also which function is used to redirect a page?
Thanks in advance :)
There's no direct equivalent.
include
and family are similar, but differ in several aspects:
- The current state of the script is kept (any global variables, function and class definition, etc.)
- A file path must be given
Probably the most similar thing is an internal Apache subrequest (see virtual
), but that has nothing to do with PHP itself. When PHP is invoked, the request has already been "dispatched". Of course, you can, if you want, do a virtual "request dispatch logic" with PHP, which means sending all the requests to some initial script that then decides what to include/execute. This is what several frameworks do.
To redirect a page, you manually send a header:
header("{$_SERVER['SERVER_PROTOCOL']} $code"); //code can be 301, 302, 303, 307
header("Location: $url");
Concepts Redirect and Forward like in Java, can be achievable in PHP too.
Redirect (URL in address bar changes ) -- header("Location: redirect.php");
Forward (URL unchanged at address bar) -- include forward.php ;
I hope, Its manageable with this & other programming logics known.
精彩评论