更改 Laravel 中的资产助手 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43493148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:49:03  来源:igfitidea点击:

Change the asset helper url in Laravel

phplaravellaravel-5.4

提问by MajAfy

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

asset()URL::asset()将指向http://my-url/public/默认。

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

有什么办法可以将 的 url 更改asset()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 assetsany time.

我有很多资产文件,我应该在我的刀片模板中使用它们,我不想在assets任何时候编写。

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

我的意思是使用asset('js/script.js')而不是asset('assets/js/scripts.js')在我的刀片模板中。

回答by Alexey Mezenin

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

覆盖默认值asset()是一个坏主意,但您可以通过定义自己的帮助程序实现来实现

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

但这是定义您自己的助手的更好方法,例如customAsset().

回答by Pankit Gami

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

回答by elyas.m

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.

我也遇到过将 S3 用作文件提供程序的情况。搜索 Laravel 文档后,我找到了以下解决方案。您可以通过在 .env 文件中设置 ASSET_URL 变量来配置资产 URL 主机。

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

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