PHP Zend Framework - How to Get Request URI Fragment from Request Object?
Say e.g. i have a URI http://127.0.开发者_运维问答0.1/somecontroller/someaction#12345
that takes me to the someAction()
action of the someController controller. From there, i am able to retrieve the Request object via $this->getRequest()
.
i am also able to retrieve various information regarding the URI from the Request object.
But, how can i retrieve the fragment (i.e. the "12345" part after the # in the e.g.)? Neither getRequestUri()
nor getParams()
turn up the fragment part.
Thanks!
The fragment part of the URL is never sent to the server via GET requests (or any kind of HTTP request for that matter), the only way you can get it is if you write a Javascript snippet that parses the URL and sends the fragment back to the server via Ajax for instance.
This can't be done with PHP alone.
According to HTTP protocol specification, the fragment part is ignored. However, browsers do support redirects with hash.
If you generate hashes automatically, you may pass the id
as the request parameter:
http://127.0.0.1/somecontroller/someaction/id/12345/#12345
and then:
$this->getRequest()->getParam('id')
But this will hot handle the case with the hash only, e.g. when user enters the URL manually.
You cannot use:
explode("#",$_SERVER['REQUEST_URI'])
because when you call $_SERVER['REQUEST_URI']
, you never get the word after #
. For example your link www.example.com/about#test
, and when you call $_SERVER['REQUEST_URI']
, you just get www.example.com/about
.
Couldn't you use the php function(s) explode("#",$_SERVER['REQUEST_URI'])?
Maybe I've misunderstood the question.
I agree with Alix Axel, but there is a way, although very dirty and ugly, this can be done, IF, and only IF, you are calling it from a web browser.
<?php
if (isset($_GET['token'])) {
die(var_dump($_GET['token']));
}
?>
<form id="teste" action="?" method="get"><input type="hidden" name="token" id="token" value=""></form>
<script>
document.querySelector('#token').value = window.location.hash;
document.querySelector('#teste').submit();
</script>
精彩评论