Yii2 integration image not found

I created a new application using Yii2 and added a new theme:

Created a directory in /var/www/html/<project_name>/web/themes/<theme_name>

Then create a subdirectory and related files.

<theme_name>
    -layouts
        main.php
    -css
        style.css
    -js
        scrippt.js
    -images
        logo.jpg

After that my topic in config/web.php

$config = [
    ..........
    'components' => [
        'view' => [
            'theme' => [
                'pathMap' => ['@app/views' => '@app/themes/<theme_name>'],
                //'baseUrl'   => '@theme/<theme_name>/'
          ]
        ],
    ...........
    ]
];

Then i changed assets/AppAsset.php

class AppAsset extends AssetBundle
{
    public $sourcePath = '@app/themes/<theme_name>';
    public $css = [
        'css/style.css'
    ];
    public $js = [
        'js/scrippt.js',
    ];
    public $depends = [

    ];
}

In main.php:

  <img src="/images/logo.jpg" alt="">

Error: logo.jpg not found.

Where is my mistake?

+4
source share
1 answer

After calling $bundle = YourAssetBundle::register($this);in the field of view, you can use $bundle->baseUrlits prefix to the image path in the image tag.

<?= Html::img($bundle->baseUrl.'/images/logo.jpg')?>

and I think /var/www/html/<project_name>/web/themes/<theme_name>it should be changed to /var/www/html/<project_name>/themes/<theme_name>, because everywhere there is an @appalias in your AssetBundle (not @web).

+3
source

Source: https://habr.com/ru/post/1584114/


All Articles