Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dmag's avatar
Level 6

asset() vs url()

What is the difference between the two and in which cases I should use one or another?

0 likes
5 replies
EliasSoares's avatar

asset() - Generate a URL to an application asset.

url() - Generate a URL to an named route.

4 likes
RachidLaasri's avatar
Level 41

the asset() method is used to include CSS/JavaScript/images files, you can use it in this cases

<link href="{{ asset('css/min.css') }}" rel="stylesheet">
<script src="{{ asset('use.typekit.net/zjb5wvv.js') }}"></script>
<img alt="logo" src="{{ asset('images/logo.png') }}">

The files must located in the public folder.

the url() method used to generate an url to a link, {{ url('users', ['page' => 22], $secure = null) }}

6 likes
MatteoOreficeIT's avatar

Both methods are helpers that point to Illuminate\Routing\UrlGenerator class' methods (asset and to), the big differences are:

asset($path, $secure = null)

  • asset doesn't accept extra query parameters
  • in case of relative URL asset checks for presence of index.php string and removes it because is not needed for assets

url/to($path, $extra = [], $secure = null)

  • to does accept extra parameters that are appended as path segments
  • to preservers query string

Try this in artisan tinker:

>>> url('relativeurl?existingparam=1',['newparam'=>2])
=> "http://phpdev/subdir/public/relativeurl/2?existingparam=1"

both

  • accept absolute path starting with \#, //, http, mailto, tel , for them it only checks for validity
5 likes

Please or to participate in this conversation.