bluesclues9
bluesclues9

Reputation: 121

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

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!

Upvotes: 6

Views: 18806

Answers (4)

lucacld
lucacld

Reputation: 11

Form me in cake 3.9 that work:

<img src="<?php echo $this->Url->build('/img/logo_app.png');  ?>" style="width:250px" />

Upvotes: 0

sanjay verma
sanjay verma

Reputation: 257

In cakephp 4.x you need to use this:

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

Upvotes: 3

ndm
ndm

Reputation: 60503

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

Upvotes: 1

Vivek
Vivek

Reputation: 223

You need to use this:

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

This will work with cakephp 3.0

Upvotes: 18

Related Questions