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

I had this piece of code in my services.ctp file that worked perfectly in CakePHP 2.3.10.

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

I just copied this file in CakePHP 3.0.0 and it no longer works and throws the following error message

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

What is the difference between this $this->webroot in CakePHP 3.0?

Please, help!

+5
source share
2 answers

You need to use this:

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

This will work with cakephp 3.0

+16
source

This is not how you should have done it in the first place, since such hardcoded URLs are very inflexible compared to arrays of URLs, where they are connected with routes that define the generated URLs at one point in your an application that allows you to easily make changes without applying modifications to the entire application.

As the saying goes, the magic $webroot missing (check the migration guide ), its value can be obtained directly through the View::$request object .

Instead, you should use Router::url() , UrlHelper or one of the HtmlHelper methods:

 \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

+1
source

Source: https://habr.com/ru/post/1215932/


All Articles