How to get $this->webroot work in CakePHP 3.0
Asked Answered
C

4

6

I had this piece of code in my services.ctp file which was working fine before in CakePHP 2.3.10.

href="<?php echo $this->webroot . 'intro/services/1'; ?>

I just copied this file into CakePHP 3.0.0 and it's no longer working and throwing the following error message

Error: C:\apache2\htdocs\myprojxxxx\webroot\Helper could not be found.

what's different with this $this->webroot in CakePHP 3.0 ?

Please help!

Cirque answered 23/3, 2015 at 17:43 Comment(0)
R
18

You need to use this:

href="<?php echo $this->request->webroot . 'intro/services/1'; ?>

This will work with cakephp 3.0

Randellrandene answered 21/4, 2015 at 7:20 Comment(0)
C
3

In cakephp 4.x you need to use this:

href="<?php echo $this->Url->webroot.'/intro/services/1'; ?>
Colander answered 17/6, 2020 at 5:17 Comment(0)
B
1

This is not how you should have done it in the first place, as such "hard-coded" URLs are very inflexible in comparison to URL arrays, where it's the connected routes that define the generated URLs at a single point in your application, allowing you to easily make changes wihout having to apply modifications throughout the whole application.

That being said, the magic $webroot property is gone (check the migration guide), its value can be retrieved directly via the View::$request object.

You should however use Router::url(), the UrlHelper, or one of the HtmlHelper methods instead:

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

See also

Brave answered 23/3, 2015 at 18:17 Comment(0)
E
0

Form me in cake 3.9 that work:

<img src="<?php echo $this->Url->build('/img/logo_app.png');  ?>" style="width:250px" />
Emergency answered 14/11, 2020 at 12:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.