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

jimmck's avatar
Level 13

What does the asset() Helper do in Blade files?

Hi,

I have noted people using asset() around links to styles and such in views? What does this asset(...) function do? I cannot find it defined in 5.1? But it pops up in earlier docs in Helpers. There is no real documentation for what it does or its arguments and return values. PHPStorm can resolve file paths, I have an assets directory in my public dir. So my views look like this, if I use a bad path or file it gets flagged:

<head>
    <title>Coverage Category</title>
    <link rel="stylesheet" href="/assets/css/main.css">
    <link rel="stylesheet" href="/assets/css/alertify.css" />
    <link rel="stylesheet" href="/assets/css/themes/default.css" />
</head>
<body>
    <script src="/assets/js/jquery-1.10.2.js"></script>
    <script src="/assets/js/knockout-3.1.0.js"></script>
    <script src="/assets/js/alertify.js"></script>
    <script src="/assets/js/ajax.js"></script>
    <script src="/assets/js/types.js"></script>
    <script src="/assets/js/fyi.js"></script>
    <script src="/assets/js/coverage_category.js"></script>
    <script src="/as

What does this helper give me??? Thanks, Jim

0 likes
12 replies
jimmck's avatar
Level 13

Thanks @mstnorris, but how does help? What does it do with the inputs? PATHS from where to where? i.e. source to destination?

Sureshkumar's avatar

It will generate a valid URL for an asset. From your code, it is OK when you host your application in root of the domain (http://www.example.com/). But if you host it in a sub folder as http://www.example.com/app/ Then your asset file attempts to load from root and will fail, but using asset() function always create application related URL.

11 likes
mstnorris's avatar

@jimmck public_path()

The public_path function returns the fully qualified path to the public directory:

$path = public_path();

Take a look at the docs

jimmck's avatar
Level 13

@mstnorris Yes. Got that. Thank You. I have noticed people creating sub-folders under there server roots? I always a create virtual host for my Laravel install make public the root dir of the host. Then I have multiple Laravel projects on one server. Since Laravel is so folder centric, seemed to be the best way?

mstnorris's avatar

@jimmck you can of course run multiple apps on the same server, but you'd use nginx configuration to do the mapping. from inside your project, those helpers will return the correct paths so you don't have to worry about it.

1 like
jimmck's avatar
Level 13

@mstnorris I keep every thing relative to the virtual host root. Each 'server host version' gets sym-linked from an 'apps' dir. But I get what you are saying. Need to be able to move builds from physical boxes quickly.

jimmck's avatar
Level 13

@mstnorris My one wish for Laravel is documentation of these functions. They do not list the arguments and expected results. And it takes time dig through the code to find out what it does. There are several mixed versions of asset() in the code. But I know I am guilty of the same thing.

mstnorris's avatar

I always ask myself "what would I want in an ideal world", I know it is what Jeffrey uses all the time. If you feel the docs are lacking, then fork them, modify what you feel is needed and submit a pull request.

With regards to what you want, find something you like and stick with it.

jimmck's avatar
Level 13

@pmall Parameter dumps are not really what I am looking for. Which is my point. If everyone has to read the source code it will hinder adoption of the library. Of course it will do wonders for Long Term Service agreements ! :)

pmall's avatar

Everyone use asset(). do as you want it doesn't matter at all ! Why are you so concerned about this ?

Please or to participate in this conversation.