Assets
Register stylesheets and scripts once, give each a nickname, and load only what a page needs. Use Vite for your own bundled frontend entry points.
Register assets by nickname
Add entries to config/css.php and config/js.php. Each entry
needs a unique nickname and a local file path beneath
public/assets. JavaScript entries can also set their script type.
return [
[
'nickname' => 'charts',
'file' => 'js/charts.min.js',
'cdn' => 'https://cdn.example.com/charts.min.js',
'default' => false,
'type' => 'text/javascript',
'attributes' => [
'defer' => true,
],
],
];
nicknameis the name used when requesting the asset from a controller.fileis the local path relative topublic/assets.cdnis an optional HTTPS URL. Set it tonullto use the local file.defaultcontrols whether the asset is included on every page.attributesadds validated HTML attributes to the stylesheet or script tag.
config/css.php, omit type. It applies to JavaScript catalog entries.
Default assets load on every page
Assets marked 'default' => true are loaded by the asset loader created
through BaseController. The shared layout renders the registered CSS in the
document head and JavaScript before the closing body tag.
[
'nickname' => 'app',
'file' => 'css/app.css',
'cdn' => null,
'default' => true,
'attributes' => [],
]
Keep page-specific libraries at false so they are not sent to every visitor.
A registered nickname is included once even if it is requested more than once.
Load optional assets in a controller
Pass the registered nickname in the third argument to $this->view().
The css and js values each accept one nickname or an array.
BaseController already has the asset loader; do not instantiate one in your controller.
public function index()
{
return $this->view('pages.reports.index', [
'title' => 'Reports',
], [
'css' => ['charts'],
'js' => ['charts'],
]);
}
BaseController. Use the
$this->view() asset argument rather than instantiating
AssetsLoader yourself.
CDN by default, local when configured
With the default auto source, an asset uses its configured CDN URL when
present. If cdn is null, it uses the local file under
public/assets. The built-in catalogs use null for local assets.
return $this->view('pages.reports.index', $data, [
'css' => ['charts'],
'js' => ['charts'],
'cssSource' => 'local',
'jsSource' => 'auto',
]);
Set cssSource or jsSource to auto,
cdn, or local. Forcing cdn requires a CDN URL
for every requested nickname; otherwise the loader reports a configuration error.
Use Vite for your app assets
Vite bundles project entry points, such as resources/js/app.js and CSS
imported by that entry. Enable it with VITE_ENABLED=true in
.env. The app layout calls vite() when enabled, using
config/vite.php inputs unless a view supplies its own entry list.
npm install
npm run dev
In development, the framework reads the Vite hot file and emits development-server tags. For production, create the manifest and compiled files:
npm run build
To choose entries for one page, provide vite in the view data (the second
argument to $this->view()). Otherwise Vite uses the inputs from
config/vite.php.
return $this->view('pages.home.index', [
'title' => 'Home',
'vite' => [
'resources/js/app.js',
],
]);
Compose page templates with layouts, sections, and reusable partials.