如何获取Cakephp的完整当前URL

Pas*_*per 54 php cakephp

你如何在Cake的视图中回显出当前的URL?

Abb*_*ant 80

你也可以

从视图文件:

<?php echo $this->here; ?>
Run Code Online (Sandbox Code Playgroud)

这将为您提供主机名的绝对URL,即/ controller/action/params

要么

<?php echo Router::url( $this->here, true ); ?> 
Run Code Online (Sandbox Code Playgroud)

哪个应该给你带主机名的完整网址.

  • 根据我的经验(CakePHP 2+,不确定其余的......) - 你应该使用`Router :: url(null,true)`而不是使用`$ this-> here`.原因是如果你必须配置一个硬盘基本路径$ this->这里将包含它,你的链接将被双重路由和破坏.只是想通过它 - 今晚让我感到困惑. (28认同)
  • 可能是$ this-> request-> here();) (4认同)
  • 另外,在cake2.x` $ this-> here`已经不够用了.您需要使用`$ this-> here()`,因为这也会附加查询字符串(从而形成完整的URL). (3认同)
  • @mark,您在 2.x 中使用的是哪个版本?我在 2.4.5 中,当我尝试使用 `here()` 方法时,出现 `PHP 致命错误:调用未定义的方法 MyController::here()`。 (2认同)

tra*_*nte 50

我更喜欢这个,因为如果我没有提到"请求"这个词,我的IDE会发出警告.

<?php echo $this->request->here; ?>
Run Code Online (Sandbox Code Playgroud)

API文档: class-CakeRequest


编辑:澄清所有选项

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/
Run Code Online (Sandbox Code Playgroud)


JJJ*_*JJJ 27

<?php echo $_SERVER[ 'REQUEST_URI' ]; ?>
Run Code Online (Sandbox Code Playgroud)

编辑:或,

<?php echo $this->Html->url( null, true ); ?>
Run Code Online (Sandbox Code Playgroud)

  • 对于可以使用基本PHP完成的所有事情,没有"蛋糕方式":)我猜你可以做`echo $ this-> Html-> url(null,true);` (4认同)

Cos*_*sta 11

以下"Cake方式"非常有用,因为您可以获取完整的当前URL并修改其中的部分内容,而无需手动解析$_SERVER[ 'REQUEST_URI' ]字符串,然后手动将其连接回有效的URL以进行输出.

完整的网址:
Router::reverse($this->request, true)

轻松修改当前URL的特定部分:
1)制作Cake的请求对象的副本: $request_copy = $this->request

2)然后修改$request_copy->params和/或 $request_copy->query数组

3)最后:$new_url = Router::reverse($request_copy, true).


And*_*eyP 8

Cakephp 3.5:

echo $this->Url->build($this->request->getRequestTarget());
Run Code Online (Sandbox Code Playgroud)

从3.4开始,不推荐调用$ this-> request-> here(),并将在4.0.0中删除.您应该使用getRequestTarget()代替.

  • 或者 `$this-&gt;getRequest()-&gt;getRequestTarget()` 更能防止弃用:D (2认同)

Sim*_*les 6

我知道这篇文章有点过时了,CakePHP版本自此繁荣起来.在目前的(2.1.x)版本的CakePHP中,甚至在1.3.x中,如果我没有弄错的话,可以得到当前的控制器/视图URL:

$this->params['url'];
Run Code Online (Sandbox Code Playgroud)

虽然此方法不返回参数,但如果您想在构建新URL时将参数附加到链接,则会很方便.例如,我们有当前的URL:

项目/编辑/ 6

我们想要附加一个名为c_action的自定义参数动作,其值为remove_image,可以使用$this->params['url];并将其与自定义参数key => value对的数组合并:

echo $this->Html->link('remove image', array_merge($this->params['url'], array('c_action' => 'remove_image'));
Run Code Online (Sandbox Code Playgroud)

使用上面的方法,我们可以将自定义参数附加到链接,而不会导致在URL上建立长链参数,因为$ this-> params ['url]只返回控制操作URL.

在上面的例子中,我们需要手动将ID 6重新添加到URL中,因此最终的链接构建可能是这样的:

echo $this->Html->link('remove image', array_merge($this->params['url'], array($id,'c_action' => 'remove_image'));
Run Code Online (Sandbox Code Playgroud)

其中$ is是项目的ID,您可以将它分配给控制器级别的变量$ id .新的URL将是:

项目/编辑/ 6/c_action:remove_image

对不起,如果这是无关紧要的,但我在寻找实现上述目标的方法时遇到了这个问题,并认为其他人可能会从中受益.


Sub*_*axe 5

在视图文件中获取当前 URL 相当简单

\n\n
echo Router::url($this->here, true);\n
Run Code Online (Sandbox Code Playgroud)\n\n

这将返回完整的 url http://www.example.com/subpath/subpath

\n\n

如果您只需要相对路径,请使用以下命令

\n\n
echo $this->here;\n
Run Code Online (Sandbox Code Playgroud)\n\n

或者

\n\n

理想情况下 Router::url(\xe2\x80\x9c\xe2\x80\x9d, true) 应返回当前视图的绝对 URL,但它始终返回相对 URL。所以获取绝对URL的技巧是

\n\n
$absolute_url  = FULL_BASE_URL + Router::url(\xe2\x80\x9c\xe2\x80\x9d, false);\n
Run Code Online (Sandbox Code Playgroud)\n\n

要获取 FULL_BASE_URL,请检查此处

\n


Ale*_*len 5

对于CakePHP 3:

$this->Url->build(null, true) // full URL with hostname

$this->Url->build(null) // /controller/action/params
Run Code Online (Sandbox Code Playgroud)


小智 5

获取CakePHP 3.x的当前URL?

在您的布局中:

<?php 
    $here = $this->request->here();
    $canonical = $this->Url->build($here, true);
?>
Run Code Online (Sandbox Code Playgroud)

您将获得当前页面的完整URL,包括查询字符串参数。

例如http://website.example/controller/action?param = value

如果需要执行一些SEO,则可以在meta标签规范中使用它。

<link rel="canonical" href="<?= $canonical; ?>">
Run Code Online (Sandbox Code Playgroud)