Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the asset helper url in Laravel

The asset() or URL::asset() will point to http://my-url/public/ in default.

Is there any way to change the url of asset() to http://my-url/public/assets/ ?

I have many asset files which I should use in my blade templates and I don't want to write assets any time.

I mean use asset('js/script.js') instead of asset('assets/js/scripts.js') in my blade templates.

like image 675
MajAfy Avatar asked Dec 10 '22 11:12

MajAfy


2 Answers

Overriding default asset() is a bad idea but you can do it by defining your own helper implementation:

function asset($path, $secure = null)
{
    return app('url')->asset($path.'/asset', $secure);
}

But it's a better way to define your own helper like customAsset().

like image 132
Alexey Mezenin Avatar answered Dec 13 '22 00:12

Alexey Mezenin


I was also in a situation to use the S3 as a file provider. After searching the Laravel document, I found the following solution. You can configure the asset URL host by setting the ASSET_URL variable in your .env file.

https://laravel.com/docs/7.x/helpers#method-asset

like image 27
elyas.m Avatar answered Dec 13 '22 02:12

elyas.m