How to find the base url of Laravel?

2

I remember that when I used the Codeigniter framework, I could figure out which was the base url of the application, simply calling base_url() . In Laravelle 3, I used URL::base() .

But now, in Laravel 5, when I call URL::base() , it returns the following error:

  

BadMethodCallException with message 'Base method does not exist.'

I tested it in Laravel 4, and the same error occurred.

So, what is the correct way to find the base url of the application, using Laravel 4 or 5?

    
asked by anonymous 24.08.2016 / 15:40

3 answers

1

There are several helpers to generate urls in Laravel >, you need to understand what would be the use case where you need only the url base.

In any case, we can use the helper url() that exists since version 4 of Laravel.

$siteRoot = url('/');

It is important to insert the slash, since the from Laravel 5.2 , the url () will return a UrlGenerator instance .

    
24.08.2016 / 16:20
1

To know the base url suffice:

$base_url = url('/');

Laravel url helpers

    
24.08.2016 / 16:12
0

In version 4 of Laravel, it is possible to do this using the URL::to() method, however it is necessary to put a bar or an empty string as a parameter.

 $base = URL::to('');

 // ou 

 $base = URL::to('/')

This will also work on version 5 of Laravel, but in this case it is best to use the url() function, as mentioned in the other answer.

    
24.08.2016 / 16:17