Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working out how Yii2 finds theme resources when using fallbacks

Tags:

php

yii

yii2

I am using Yii2 and have been reading about theming and theme inheritance; however have some questions:

Consider the following example:

'view' => [
    'theme' => [
        'pathMap' => [
            '@app/views' => [
                '@app/themes/current',
                '@app/themes/default',
            ],
        ],
        'baseUrl' => '@web/themes/current',
        'basePath' => '@webroot/themes/current',
    ],
],

Now, imagine we request the theme file foo; as I understand it this will first be looked for in the following order:

  • @app/themes/current/foo.php
  • @app/themes/default/foo.php
  • @app/views/foo.php

Now imagine foo.php isn't found in the @app/themes/current/ theme, so it would use the file found in @app/themes/default/.

Now, considering the baseUrl and basePath settings I am a little confused how these are used in these situations.

Now, imagine foo.php references an image file inside the file, wouldn't this still attempt to find @web/themes/current/images/myImage.jpg rather than @web/themes/default/images/myImage.jpg?

like image 987
Brett Avatar asked Aug 17 '15 10:08

Brett


2 Answers

In this case, basePath is worthless. Because basePath is only applied when pathMap is empty.
basePath is not a fallback, It is a shortcut of pathMap, quick use when you only have one theme.

'pathMap' => [
    '@app/views' => [
         '@app/themes/current/views',
    ],
],

Equivalent to:

'basePath' => '@app/themes/current', 

(Yii understands that folder @app/themes/current/views exist)

baseUrl: It is returned when you call $this->theme->getBaseUrl() in view file. Less worth with multi theme.

About fallback for static files. Theming fallback is not designed for this purpose.

like image 61
Ngô Văn Thao Avatar answered Nov 05 '22 12:11

Ngô Văn Thao


Point your links inside file like this exmaple from docs:

$theme = $this->theme;

// returns: $theme->baseUrl . '/img/logo.gif'
$url = $theme->getUrl('img/logo.gif');

// returns: $theme->basePath . '/img/logo.gif'
$file = $theme->getPath('img/logo.gif');

It will get file from current theme directory.

like image 26
SiZE Avatar answered Nov 05 '22 12:11

SiZE