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

17,637

Solution 1

You need to use this:

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

This will work with cakephp 3.0

Solution 2

In cakephp 4.x you need to use this:

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

Solution 3

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

Share:
17,637
bluesclues9
Author by

bluesclues9

Updated on June 19, 2022

Comments

  • bluesclues9
    bluesclues9 almost 2 years

    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!