Search code examples
laravelurllaravel-5laravel-facade

Can't generate absolute path using URL::to() in laravel 5.2


I'm using Laravel 5.2 and I wanted to give my stylesheet an absolute path

<link rel="stylesheet" href="{{ URL::to('css/styles.css') }}">  

the stylesheet can't be loaded . when i use href="{{ asset('css/styles.css') }}" it works just fine but when i use the facade URL::to() it doesn't load .


Solution

  • URL::to() will return an absolute path. Do you have index.php in your url? In your example, the only real difference between URL::to() and asset() is that the asset() method will remove any reference to index.php, but URL::to() won't.

    If you want to use the URL facade instead of the global helper methods, I would suggest using URL::asset() instead of URL::to(). That is the function specific for loading assets, and is actually what the asset() helper function calls.