PHP Zend Framework - 如何从请求对象获取请求 URI 片段?

发布于 2024-08-20 02:32:07 字数 355 浏览 4 评论 0原文

假设我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陌路黄昏 2024-08-27 02:32:07

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.

无远思近则忧 2024-08-27 02:32:07

根据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.

若水微香 2024-08-27 02:32:07

您不能使用:,

explode("#",$_SERVER['REQUEST_URI'])

因为当您调用 $_SERVER['REQUEST_URI'] 时,您永远不会得到 # 之后的单词。例如,您的链接 www.example.com/about#test,当您调用 $_SERVER['REQUEST_URI'] 时,您只会得到 www.example。 com/about

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.

仙气飘飘 2024-08-27 02:32:07

你不能使用php函数explode("#",$_SERVER['REQUEST_URI'])吗?也许我误解了这个问题。

Couldn't you use the php function(s) explode("#",$_SERVER['REQUEST_URI'])? Maybe I've misunderstood the question.

给我一枪 2024-08-27 02:32:07

我同意Alix Axel,但是有一种方法,虽然很肮脏和丑陋,但这可以做到,如果,并且仅当您从 Web 浏览器调用它时。

<?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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文