简体   繁体   English

Laravel:在刀片页面中使用资产方法有什么好处?

[英]Laravel: What's the advantage of using the asset method in blade pages?

In some laravel examples I've seen assets being called like this: 在一些laravel示例中,我看到资产被调用如下:

<link rel="icon" type="image/png" href="{{ asset('icons/favicon-32x32.png') }}" sizes="32x32">

What is the advantage of using that method as opposed to doing this? 使用该方法的优势是什么,而不是这样做?

<link rel="icon" type="image/png" href="icons/favicon-32x32.png" sizes="32x32">

If you do the latter, the URLs will not work when you're on any URL that includes a forward slash. 如果您执行后者,当您使用包含正斜杠的任何URL时,URL将不起作用。 Eg not using asset will appear to work fine if you're on the homepage, but if you were on /search/results or whatever, the URLs won't be correct (as it'll be looking for /search/icons/favicon-32x32.png ). 例如,如果你在主页上,不使用asset似乎工作正常,但如果你在/search/results或其他什么,URL将是不正确的(因为它将寻找/search/icons/favicon-32x32.png )。

It helps you to generate a valid URL for your assets. 它可以帮助您为资产生成有效的URL。

If you use asset('icons/favicon-32x32.png') , it will always create application related URL's irrespective of the position of the installation folder of your app in the server. 如果您使用asset('icons/favicon-32x32.png') ,它将始终创建与应用程序相关的URL,而不管您的应用程序的安装文件夹在服务器中的位置。

For example, if your application is hosted in a subfolder instead of the root directory, if you use asset method, laravel will create the related path for the asset. 例如,如果您的应用程序托管在子文件夹而不是根目录中,则使用asset方法时,laravel将创建资产的相关路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM