Laravel 4 URL::asset() 和 asset() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20559353/
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 08:45:35  来源:igfitidea点击:

Laravel 4 difference between URL::asset() and asset()

laravellaravel-4blade

提问by Javier Cadiz

For loading assets in Laravel 4 projects there is a helperto create a URL for an asset

为了在 Laravel 4 项目中加载资产,有一个帮助程序可以为资产创建 URL

<link rel="stylesheet" href="{{ asset('css/styles.css') }}" />

But that helper could be called using a facade too

但是也可以使用外观调用该助手

<link rel="stylesheet" href="{{ URL::asset('css/styles.css') }}" />

which produce the same result.

产生相同的结果。

So my question is, which is the real difference here, one way is better in terms of performance than the other or is just a preference style ??

所以我的问题是,这是真正的区别,一种方式在性能方面比另一种方式更好,或者只是一种偏好风格?

回答by Mike Rockétt

This is the asset()function:

这是asset()函数:

if ( ! function_exists('asset'))
{
    /**
     * Generate an asset path for the application.
     *
     * @param  string  $path
     * @param  bool    $secure
     * @return string
     */
    function asset($path, $secure = null)
    {
        return app('url')->asset($path, $secure);
    }
}

Both of the functions are, therefore, the same. asset()is simply a helper function. Specifically, helpers are more appropriate for views. So, yes, it is a preference thing. I tend to prefer using Facades.

因此,这两个函数是相同的。asset()只是一个辅助函数。具体来说,助手更适合视图。所以,是的,这是一个偏好的东西。我倾向于使用 Facades。

回答by Hao Luo

they are the same. the helper function is just an alias.

他们是一样的。辅助函数只是一个别名。