PHP Zend Framework - 如何从请求对象获取请求 URI 片段?
假设我有一个 URI http://127.0.0.1/somecontroller/someaction#12345
,它将我带到 someController 控制器的 someAction()
操作。从那里,我可以通过 $this->getRequest()
检索 Request 对象。
我还能够从 Request 对象中检索有关 URI 的各种信息。
但是,我如何检索片段(即例如中#后面的“12345”部分)? getRequestUri()
和 getParams()
都没有显示片段部分。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
URL 的片段部分永远不会通过 GET 请求(或任何类型的 HTTP 请求)发送到服务器,获取它的唯一方法是编写一个 Javascript 片段来解析 URL 并将片段发回例如通过 Ajax 到服务器。
仅靠 PHP 无法完成此任务。
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.
根据HTTP协议规范,片段部分被忽略。但是,浏览器确实支持使用哈希进行重定向。
如果您自动生成哈希值,则可以将
id
作为请求参数传递:http://127.0.0.1/somecontroller/someaction/id/12345/#12345
然后:
$this->getRequest()->getParam('id')
code>但这将热处理仅使用哈希值的情况,例如,当用户手动输入 URL 时。
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.
您不能使用:,
因为当您调用
$_SERVER['REQUEST_URI']
时,您永远不会得到#
之后的单词。例如,您的链接www.example.com/about#test
,当您调用$_SERVER['REQUEST_URI']
时,您只会得到www.example。 com/about
。You cannot use:
because when you call
$_SERVER['REQUEST_URI']
, you never get the word after#
. For example your linkwww.example.com/about#test
, and when you call$_SERVER['REQUEST_URI']
, you just getwww.example.com/about
.你不能使用php函数
explode("#",$_SERVER['REQUEST_URI'])吗?
也许我误解了这个问题。Couldn't you use the php function(s)
explode("#",$_SERVER['REQUEST_URI'])?
Maybe I've misunderstood the question.我同意Alix Axel,但是有一种方法,虽然很肮脏和丑陋,但这可以做到,如果,并且仅当您从 Web 浏览器调用它时。
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.