Javier Cadiz
Javier Cadiz

Reputation: 12536

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

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

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

Upvotes: 7

Views: 17660

Answers (2)

Mike Rock&#233;tt
Mike Rock&#233;tt

Reputation: 9007

This is the asset() function:

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.

Upvotes: 12

Hao Luo
Hao Luo

Reputation: 1891

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

Upvotes: 2

Related Questions