0

I am using the below code in my Controller file to generate full site URL

$this->Url->build(['controller' => 'home', 'action' => 'index'], true);.

But I am getting a "Call to a member function build() on boolean" error. The reason why I am not using

Router::URL(['controller' => 'home', 'action' => 'index']);

is I don't want a relative URL. Can't I use Url builder in the controller?

web_developer
  • 71
  • 1
  • 9
  • 1
    Helpers are for views, not controllers. `Router::url()` will give you the absolute/full URL when passing `true` as the second argument, so you might need to explain a little more detailed how the snippet you're showing there doesn't give you the desired result. – ndm May 15 '20 at 14:04
  • Thank you @ndm. I wasn't passing ````true```` in ````Router::URL````. I have edited my question. – web_developer May 15 '20 at 14:11

1 Answers1

0

I think, the main reason of your Error is wrong name of Controller.

First,Name Conventions says

"Controller class names are plural, PascalCased, and end in Controller. UsersController and ArticleCategoriesController are both examples of conventional controller names."

so your file name must be like HomesController.php. Next the Class name should be like this HomesController.

Secondly, for CakePHP character size matters. If you want build URL to your controller you have to do like this

$this->Url->build(['controller' => 'Home', 'action' => 'index'], true);

Where controller name should be capitalize.

More about building URL in CakePHP

Oliwer Lisek
  • 126
  • 5