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

只谈情不闲聊 提交于 2019-12-22 04:32:07

问题


In some laravel examples I've seen assets being called like this:

<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">

回答1:


If you do the latter, the URLs will not work when you're on any URL that includes a forward slash. E.g. 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).




回答2:


It helps you to generate a valid URL for your assets.

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.

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.



来源:https://stackoverflow.com/questions/35896303/laravel-whats-the-advantage-of-using-the-asset-method-in-blade-pages

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