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
Any file placed there is served directly by MageWork, with an automatic Content-Type
(requires ext-fileinfo). A file whose name starts with a dot is never served.
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'));
- Installation
- Configuration
- Add a new package
- Add a new HTML page
- Serve any type of file
- Assets
- Rewrite a route
- Add a new block
- Data assignment
- Objects and class fallback
- Models
- Database
- Console Commands
- Framework tools
- Custom shared libraries
- External libraries with composer
- Templating best practices
- Hooks
- Forms
- Session messages
- Send emails
- Captcha
- Write content in Markdown
- Static Site Generator