8

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.

MajAfy
  • 3,007
  • 10
  • 47
  • 83

4 Answers4

7

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

elyas.m
  • 1,260
  • 1
  • 13
  • 20
6

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().

Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
4

according to laravel 8.x documentation you can configure the asset URL host by setting the ASSET_URL variable in your .env file.

in your case in your .env file:

ASSET_URL=/public/assets

if changes do not take place, remember to run php artisan config:clear

Jorge Peralta
  • 67
  • 1
  • 4
3

By default, there isn't any configuration for that. You can create your custom function like this:

function my_asset($path, $secure = null){
    return asset('/assets/' . trim($path, '/'), $secure);
}
Pankit Gami
  • 2,523
  • 11
  • 19