Change the asset helper url in Laravel

假装没事ソ 提交于 2020-05-13 14:02:28

问题


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.


回答1:


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);
}



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/43493148/change-the-asset-helper-url-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!