Assets

HTML is just not enough to render a modern website. JavaScript, CSS, images and videos play a big role in the client experience. Bear Framework enables you to handle such assets with ease.

The $app->assets object enables to get a public URL for a specific asset. You can even resize images on the fly. The only requirement is that files must be in a registered directory (see below) or made public if they are in the data storage.

API

BearFramework\App\Assets - Provides utility functions for assets
BearFramework\App\Assets::addDir - Registers a directory that will be publicly accessible
BearFramework\App\Assets::getFilename - Returns the local filename for a given URL path
BearFramework\App\Assets::getMimeType - Finds the mime type of a filename by checking it's extension
BearFramework\App\Assets::getUrl - Returns a public URL for the specified filename

Files in the app or addon directories

This is how you can get the URL of an asset in your app directory

// You can mark a directory to be publicly accessible and get file url
$app->assets->addDir('../app/assets/');
$app->assets->getUrl('../app/assets/logo.png');

// It's better to use the config value
$app->assets->addDir($app->config->appDir . 'assets/');
$app->assets->getUrl($app->config->appDir . 'assets/logo.png');

// Or even better you can use the $context object
$context->assets->addDir('assets/');
$context->assets->getUrl('assets/logo.png');

Files in the data storage

This is how you can get the URL of an asset from your data storage.

// You can use the full data path
$app->assets->getUrl($app->data->getFilename('uploads/file1.jpg'));

It's important that the object is marked as public.

$app->data->makePublic([
    'key' => 'uploads/file1.jpg'
]);

Image resizing

In the options argument you can specify the width or the height or both of the desired image.

$context->assets->getUrl('assets/logo.png', ['width' => 300]); // height is automatically calculated
$context->assets->getUrl('assets/logo.png', ['width' => 300, 'height' => 200]);
Next: Cache