MageWork

Back to home

Assets

Asset directory

Public files (CSS, JS, images, fonts, robots.txt, favicon.ico...) live in the package asset directory:

pub > assets > Acme > css > style.css
pub > assets > Acme > js > app.js
pub > assets > Acme > media > logo.png

Assets are served by the web server from their asset URL (/assets/Acme/css/style.css).

Files at the root of the package asset directory (robots.txt, favicon.ico, site verification files...) are also served by MageWork at the root of the package URL (/robots.txt), with an automatic Content-Type (requires ext-fileinfo). Files in subdirectories, hidden files (starting with a dot), symbolic links and PHP files are never served this way.

Asset URL

In a template, build the URL with getAssetUrl(). The path is relative to the current package asset directory:

<link rel="stylesheet" href="<?= $this->getAssetUrl('css/style.css') ?>" type="text/css" />
<img src="<?= $this->getAssetUrl('media/logo.png') ?>" alt="Acme" />

Query parameters

The second argument adds query parameters, for example to bust the browser cache:

<link rel="stylesheet" href="<?= $this->getAssetUrl('css/style.css', ['v' => '2.1.0']) ?>" type="text/css" />

Asset of another package

The third argument loads an asset from another package:

<img src="<?= $this->getAssetUrl('media/logo.png', [], 'Admin') ?>" alt="Admin" />

Asset file path

App::getAssetPath() returns the absolute file path of an asset (not a URL), useful to
read a file on disk:

<?php

$size = filesize(App::getAssetPath('media/logo.png'));