PHP Zend Framework - 如何从请求对象中获取请求 URI 片段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2181290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Zend Framework - How to Get Request URI Fragment from Request Object?
提问by Edwin Lee
Say e.g. i have a URI http://127.0.0.1/somecontroller/someaction#12345that takes me to the someAction()action of the someController controller. From there, i am able to retrieve the Request object via $this->getRequest().
比如说,我有一个 URI http://127.0.0.1/somecontroller/someaction#12345,它将我带到someAction()someController 控制器的操作。从那里,我可以通过$this->getRequest().
i am also able to retrieve various information regarding the URI from the Request object.
我还可以从 Request 对象中检索有关 URI 的各种信息。
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.
但是,我如何检索片段(例如,在 # 之后的“12345”部分)?既不打开getRequestUri()也不getParams()打开片段部分。
Thanks!
谢谢!
回答by Alix Axel
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.
URL 的片段部分永远不会通过 GET 请求(或任何类型的 HTTP 请求)发送到服务器,唯一可以获得它的方法是编写一个 Javascript 片段来解析 URL 并将片段发回例如通过 Ajax 到服务器。
This can't be done with PHP alone.
这不能单独使用 PHP 来完成。
回答by takeshin
According to HTTP protocol specification, the fragment part is ignored. However, browsers do support redirects with hash.
根据 HTTP 协议规范,片段部分被忽略。但是,浏览器确实支持使用哈希重定向。
If you generate hashes automatically, you may pass the idas the request parameter:
http://127.0.0.1/somecontroller/someaction/id/12345/#12345
如果您自动生成哈希,您可以将 传递id为请求参数:
http://127.0.0.1/somecontroller/someaction/id/12345/#12345
and then:
进而:
$this->getRequest()->getParam('id')
$this->getRequest()->getParam('id')
But this will hot handle the case with the hash only, e.g. when user enters the URL manually.
但这将仅使用哈希处理这种情况,例如,当用户手动输入 URL 时。
回答by julius
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.
因为当你打电话时$_SERVER['REQUEST_URI'],你永远不会得到这个词#。例如你的链接www.example.com/about#test,当你打电话时$_SERVER['REQUEST_URI'],你就得到了www.example.com/about。
回答by Ben Muncey
Couldn't you use the php function(s) explode("#",$_SERVER['REQUEST_URI'])?Maybe I've misunderstood the question.
你不能使用 php 函数吗?explode("#",$_SERVER['REQUEST_URI'])?也许我误解了这个问题。

![PHP $_SERVER['DOCUMENT_ROOT']](/res/img/loading.gif)