Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Laravel 5, What's the difference between {{url}} and {{asset}}?

Tags:

I find sometimes both is OK? So what's the really difference?

For example,

<link rel="stylesheet" href="{{asset('resources/views/admin/style/css-ui.admin.css')}}"> 

and

<link rel="stylesheet" href="{{url('resources/views/admin/style/font/css/font-awesome.min.css')}}"> 

these two form is both OK.

So, what's the differences?

like image 692
无名小路 Avatar asked Aug 30 '16 02:08

无名小路


People also ask

What is asset () in laravel?

Laravel provides a helper function, asset() , which generates a URL for your assets. You can use this in blade syntax, e.g. <script type="text/javascript" src="{ { asset('js/jquery.js') } }"></script>

What is URL function in laravel?

Laravel allows you to easily create "signed" URLs to named routes. These URLs have a "signature" hash appended to the query string which allows Laravel to verify that the URL has not been modified since it was created.

How do I change the assets URL in laravel?

You can configure the asset URL host by setting the ASSET_URL variable in your . env file. Show activity on this post. Show activity on this post.

How do I find base URL in laravel?

echo base_url(); to get my site's URL.


1 Answers

Deciding which URL helper to use

Consider the type of URL that is needed / how the URL is being used. One of the advantages of having separate helper methods for each type of URL is they can have different handling logic. For example, assets (e.g. CSS, images, etc.) could involve a check that the file exists in the file system but do not require the type of analysis that a route would because the route may have parameters.

url() Generates an absolute URL to the given path (code)

  • Use for static URLs (which should be rare).
  • Accepts array of parameters that are encoded and added to the end of the domain.
  • Preserves any URL query string.

    {{ url('search') }} // http://www.example.com/search  {{ url('search', ['qevo', 'laravel']) }} // http://www.example.com/search/qevo/laravel 

asset() Generates a URL to an application asset (code)

  • Use for files that are directly served such as CSS, images, javascript.
  • Only accepts a direct path.

    {{ asset('css/app.css') }} // http://www.example.com/css/app.css 

route() Gets the URL to a named route (code)

  • Use for every route (every route should be named to help future-proof path changes).
  • Requires named routes.
  • Accepts associative array for route parameters.
  • Allows override for relative route vs. absolute route (default).

    {{ route('user.profile', ['name'=>'qevo']) }} // http://www.example.com/user/qevo/profile  {{ route('user.profile', ['name'=>'qevo'], false) }} // /user/qevo/profile 
like image 78
Qevo Avatar answered Oct 10 '22 13:10

Qevo