php 如何获取 Cakephp 的完整当前 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6836990/
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
How to get complete current url for Cakephp
提问by Passionate Engineer
How do you echo out current URL in Cake's view?
你如何在 Cake 的视图中回显当前 URL?
回答by Abba Bryant
You can do either
你可以这样做
From a view file:
从视图文件:
<?php echo $this->here; ?>
Which will give you the absolute url from the hostname i.e. /controller/action/params
这将为您提供来自主机名的绝对网址,即 /controller/action/params
Or
或者
<?php echo Router::url( $this->here, true ); ?>
which should give you the full url with the hostname.
这应该为您提供带有主机名的完整网址。
回答by trante
I prefer this, because if I don't mention "request" word, my IDE gives warning.
我更喜欢这个,因为如果我不提到“请求”这个词,我的 IDE 就会发出警告。
<?php echo $this->request->here; ?>
API Document: class-CakeRequest
API 文档: class-CakeRequest
Edit: To clarify all options
编辑:澄清所有选项
Current URL: http://example.com/en/controller/action/?query=12
// Router::url(null, true)
http://example.com/en/controller/action/
// Router::url(null, false)
/en/controller/action/
// $this->request->here
/en/controller/action/
// $this->request->here()
/en/controller/action/?query=12
// $this->request->here(false)
/en/controller/action/?query=12
// $this->request->url
en/controller/action
// $_SERVER["REQUEST_URI"]
/en/controller/action/?query=12
// strtok($_SERVER["REQUEST_URI"],'?');
/en/controller/action/
回答by JJJ
<?php echo $_SERVER[ 'REQUEST_URI' ]; ?>
EDIT: or,
编辑:或者,
<?php echo $this->Html->url( null, true ); ?>
回答by Costa
The following "Cake way" is useful because you can grab the full current URL and modify parts of it without manually having to parse out the $_SERVER[ 'REQUEST_URI' ]
string and then manually concatenating it back into a valid url for output.
下面的“蛋糕方式”很有用,因为您可以获取完整的当前 URL 并修改其中的一部分,而无需手动解析$_SERVER[ 'REQUEST_URI' ]
字符串,然后手动将其连接回有效的 url 以进行输出。
Full current url:Router::reverse($this->request, true)
完整的当前网址:Router::reverse($this->request, true)
Easily modifying specific parts of current url:
1) make a copy of Cake's request object:
$request_copy = $this->request
轻松修改当前 url 的特定部分:
1) 复制 Cake 的请求对象:
$request_copy = $this->request
2) Then modify $request_copy->params
and/or $request_copy->query
arrays
2)然后修改$request_copy->params
和/或 $request_copy->query
数组
3) Finally: $new_url = Router::reverse($request_copy, true)
.
3)最后:$new_url = Router::reverse($request_copy, true)
。
回答by AndreyP
Cakephp 3.5:
Cakephp 3.5:
echo $this->Url->build($this->request->getRequestTarget());
Calling $this->request->here() is deprecated since 3.4, and will be removed in 4.0.0. You should use getRequestTarget() instead.
自 3.4 起不推荐调用 $this->request->here(),并将在 4.0.0 中删除。您应该改用 getRequestTarget()。
回答by SimonDowdles
I know this post is a little dated, and CakePHP versions have flourished since. In the current (2.1.x) version of CakePHP and even in 1.3.x if I am not mistaken, one can get the current controller/view url like this:
我知道这篇文章有点过时了,而且 CakePHP 版本从那以后蓬勃发展。在当前 (2.1.x) 版本的 CakePHP 中,甚至在 1.3.x 中,如果我没记错的话,可以像这样获得当前的控制器/视图 url:
$this->params['url'];
While this method does NOT return the parameters, it is handy if you want to append parameters to a link when building new URL's. For example, we have the current URL:
虽然此方法不返回参数,但如果您想在构建新 URL 时将参数附加到链接,它会很方便。例如,我们有当前的 URL:
projects/edit/6
项目/编辑/6
And we want to append a custom parameter action called c_action with a value of remove_image, one could make use of $this->params['url];
and merge it with an array of custom parameter key => value pairs:
我们想要附加一个名为 c_action 的自定义参数操作,其值为 remove_image,可以使用$this->params['url];
它并将其与一组自定义参数键 => 值对合并:
echo $this->Html->link('remove image', array_merge($this->params['url'], array('c_action' => 'remove_image'));
Using the above method we are able to append our custom parameters to the link and not cause a long chain on parameters to build up on the URL, because $this->params['url] only ever returns the controll action URL.
使用上面的方法,我们能够将自定义参数附加到链接上,并且不会导致在 URL 上建立长长的参数链,因为 $this->params['url] 只返回控制操作 URL。
In the above example we'd need to manually add the ID of 6 back into the URL, so perahaps the final link build would be like this:
在上面的示例中,我们需要手动将 6 的 ID 添加回 URL,因此最终链接构建可能是这样的:
echo $this->Html->link('remove image', array_merge($this->params['url'], array($id,'c_action' => 'remove_image'));
Where $is is a the ID of the project and you would have assigned it to the variable $id at controller level. The new URL will then be:
其中 $is 是项目的 ID,您可以将它分配给控制器级别的变量 $id 。新的 URL 将是:
projects/edit/6/c_action:remove_image
项目/编辑/6/c_action:remove_image
Sorry if this is in the slightest unrelated, but I ran across this question when searching for a method to achieve the above and thought others may benefit from it.
对不起,如果这有丝毫不相关,但我在寻找实现上述目标的方法时遇到了这个问题,并认为其他人可能会从中受益。
回答by Alex Stallen
for CakePHP 3:
对于 CakePHP 3:
$this->Url->build(null, true) // full URL with hostname
$this->Url->build(null) // /controller/action/params
回答by Guillaume
Getting current URL for CakePHP 3.x ?
获取 CakePHP 3.x 的当前 URL ?
In your layout :
在您的布局中:
<?php
$here = $this->request->here();
$canonical = $this->Url->build($here, true);
?>
You will get the full URL of the current page including query string parameters.
您将获得当前页面的完整 URL,包括查询字符串参数。
e.g. http://website.example/controller/action?param=value
例如http://website.example/controller/action?param=value
You can use it in a meta tag canonical if you need to do some SEO.
如果您需要做一些 SEO,您可以在元标记规范中使用它。
<link rel="canonical" href="<?= $canonical; ?>">
回答by Subodh Ghulaxe
Getting the current URL is fairly straight forward in your view file
在您的视图文件中获取当前 URL 相当简单
echo Router::url($this->here, true);
This will return the full url http://www.example.com/subpath/subpath
这将返回完整的 url http://www.example.com/subpath/subpath
If you just want the relative path, use the following
如果您只想要相对路径,请使用以下内容
echo $this->here;
OR
或者
Ideally Router::url(“”, true) should return an absolute URL of the current view, but it always returns the relative URL. So the hack to get the absolute URL is
理想情况下 Router::url(“”, true) 应该返回当前视图的绝对 URL,但它总是返回相对 URL。所以获取绝对 URL 的技巧是
$absolute_url = FULL_BASE_URL + Router::url(“”, false);
To get FULL_BASE_URL check here
要获得 FULL_BASE_URL 检查这里
回答by giuseppe
In the request object you have everything you need. To understand it:
在请求对象中,您拥有所需的一切。要理解它:
debug($this->request->url);
and in your case
在你的情况下
$here = $this->request->url;