When creating a Laravel 5 package, how do I load assets like CSS/JS files? My working package currently has controllers and views, but all the CSS is placed in the header of each view which isn't good.
Edit: Thanks for your answers, but perhaps I wasn't being too clear. My question is in reference to a Laravel package and not a Laravel app. In this case, I load views using Service Providers
// Views
$this->loadViewsFrom(__DIR__ . '/Views', 'packagename');
The above line is placed in the register()
method.
According to the docs:
The
public
directory contains the front controller and your assets (images, JavaScript, CSS, etc.).
My usual structure is as follows:
/public
/css
/js
index.php
Then, easily in your blade template you can add those assets like:
<link rel="stylesheet" href="css/main.css">
Recently I found that in some cases this will cause a 404. To insure you always get the resource loaded, you can use:
<link rel="stylesheet" href="{{ asset('css/main.css') }}" >
The docs for this can be found here, and you can refer to this previously answered question regarding this issue.