如何在CakePHP 3.0中获取$ this-> webroot

blu*_*es9 5 cakephp cakephp-3.0

我在我的services.ctp文件中有这段代码,之前在CakePHP 2.3.10中工作正常.

href="<?php echo $this->webroot . 'intro/services/1'; ?>
Run Code Online (Sandbox Code Playgroud)

我刚刚将此文件复制到CakePHP 3.0.0中,它不再工作并抛出以下错误消息

错误:找不到C:\ apache2\htdocs\myprojxxxx\webroot\Helper.

$this->webrootCakePHP 3.0中有什么不同之处?

请帮忙!

Viv*_*vek 18

你需要使用这个:

href="<?php echo $this->request->webroot . 'intro/services/1'; ?>
Run Code Online (Sandbox Code Playgroud)

这将适用于cakephp 3.0


ndm*_*ndm 1

这不是你一开始就应该做的,因为与 URL 数组相比,这种“硬编码”的 URL 非常不灵活,在 URL 数组中,连接的路由在应用程序中的单个点定义生成的 URL,从而允许您可以轻松地进行更改,而无需在整个应用程序中应用修改。

虽这么说,魔法$webroot属性已经消失了(查看迁移指南),它的值可以直接通过对象View::$request检索。

但是,您应该改用Router::url()、 theUrlHelper或 其中一种HtmlHelper方法:

\Cake\Routing\Router::url(['controller' => 'Intro', 'action' => 'services', 1])
Run Code Online (Sandbox Code Playgroud)
$this->Url->build(['controller' => 'Intro', 'action' => 'services', 1])
Run Code Online (Sandbox Code Playgroud)
$this->Html->link('Title', ['controller' => 'Intro', 'action' => 'services', 1])
Run Code Online (Sandbox Code Playgroud)

也可以看看