Laravel 5: Generating URLs to Assets With asset

April 14, 2018 —John Koster

The asset function will return a URI composed of the application's URI and the given $path. If $secure is true, the returned URI will have the https:// schema, otherwise it will have the http:// schema.

The application URI used to generate the final URI is what was used to issue the request, and typically matches what appears in the browser's address bar.

#Signature

The signature of the asset function is:

1function asset(
2 $path,
3 $secure = null
4);

#Example Use

Assuming the application path is laravel.artisan the following asset paths can be generated (the resulting URI appears above the function call as a comment):

1// http://laravel.artisan/test
2asset('test');
3 
4// http://laravel.artisan/js/application.min.js
5asset('js/application.min.js');
6 
7// http://laravel.artisan/js/application.min.js
8asset('http://laravel.artisan/js/application.mins.js');

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.