Skip to content

Latest commit

 

History

History
59 lines (41 loc) · 1.43 KB

README.md

File metadata and controls

59 lines (41 loc) · 1.43 KB

Using assets

Assets are files that Webpack does not process.

They are copied directly to the output folder.

This includes images, fonts, and any other files that you want to use in your project.

To use an asset, you need to import it from JavaScript or CSS.

Register the asset bundle with a view component such as a layout view. You can register it in a layout view so that it is available in all views that extend from this layout, or you can register it in a view that uses the asset.

file: ./resources/views/layout/main.php

<?php

declare(strict_types=1);

use Yii\Asset\BootstrapAsset;
use Yiisoft\Assets\AssetManager;

/**
 * @var AssetManager $assetManager
 */

// Register the asset bundle with an asset manager component.
$assetManager->register(BootstrapAsset::class);

// Set parameters for the registered asset bundle a view component.
$this->addCssFiles($assetManager->getCssFiles());
$this->addCssStrings($assetManager->getCssStrings());
$this->addJsFiles($assetManager->getJsFiles());
$this->addJsStrings($assetManager->getJsStrings());
$this->addJsVars($assetManager->getJsVars());

Also, you can register the asset bundle via container configuration:

file: ./config/params.php

<?php

declare(strict_types=1);

use Yii\Asset\BootstrapCdnAsset;

return [
    'yiisoft/assets' => [
        'assetManager' => [
            'register' => [
                BootstrapCdnAsset::class,
            ],
        ],
    ],
];