N
N
Nikolai2016-03-09 09:20:22
Yii
Nikolai, 2016-03-09 09:20:22

How to make your own themes for a yii2 project?

I read the documentation.
Base template.
I created the themes folder in it mythemes, transferred the standard layouts from main.php to it from views.
In config:

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

In principle, the theme works, views from the root generally cut out.
I'm trying to add my css for a simple color change. I create /themes/mythemes/css/style.css, added to /themes/mythemes/layouts/main.php:
<link href="<?php echo $this->theme->baseUrl ?>/css/style.css" rel="stylesheet" media="screen" title="default">

Instead of /themes/mythemes/css/style.css , it returns a page with a 404 error.
I tried a bunch of options, it doesn't work, damn it.
How to "throw" static files from @app to @web? Not only CSS, but also pictures, for example. Or tell me another way, so that the topic is in the same folder.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Voronkov, 2016-03-09
@Nikudator

This is how it works in the first version. In the second version, Doka rules with scripts and stylesassets/AppAsset.php

N
Nikolai, 2016-03-09
@Nikudator

In general, the sequence I stopped at is this:
Create themes/mytheme
In it, copy the layouts folder from the view with main.php contained in it
In config/web.php

'components' => [
        'view' => [
            'theme' => [
                'pathMap' => ['@app/views' => '@app/themes/mytheme'],
            ],
        ],
    ],

Create themes/mytheme/css/style.css
And in assets/AppAsset.php change the content of the class to:
class AppAsset extends AssetBundle
{
    public $sourcePath = '@app/themes/mytheme';
    public $css = [
        'css/style.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

A
Alexander N++, 2016-03-09
@sanchezzzhak

i use a crutch

<?php
namespace app\components;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\FileHelper;

class Theme extends \yii\base\Theme
{

    public $active;
    /**
     * @inheritdoc
     */
    public function applyTo($path)
    {

        $pathMap = ArrayHelper::getValue($this->pathMap,$this->active,$this->pathMap);

        if (empty($pathMap)) {
            if (($basePath = $this->getBasePath()) === null) {
                throw new InvalidConfigException('The "basePath" property must be set.');
            }
            $pathMap = [Yii::$app->getBasePath() => [$basePath]];
        }


        $path = FileHelper::normalizePath($path);
        foreach ($pathMap as $from => $tos) {
            $from = FileHelper::normalizePath(Yii::getAlias($from)) . DIRECTORY_SEPARATOR;
            if (strpos($path, $from) === 0) {
                $n = strlen($from);
                foreach ((array) $tos as $to) {
                    $to = FileHelper::normalizePath(Yii::getAlias($to)) . DIRECTORY_SEPARATOR;
                    $file = $to . substr($path, $n);
                    if (is_file($file)) {
                        return $file;
                    }
                }
            }
        }
        return $path;
    }
}

since switching templates from controller doesn't work, carl!!!
in configs
'view'=>[

            'theme' => [
                'class'=>'app\components\Theme',
                'active'=>'theme-default',
                'pathMap' => [
                    'theme-default' => [
                        '@app/views' => ['@app/themes/theme-default/views']
                    ],
                    'theme-dashboard' => [
                        '@app/views' => ['@app/themes/theme-dashboard/views']
                    ]
                ]
            ],
        ],

In the controller, the theme is specified as follows.
If there are no views in the theme folder, then the search will be searched in the standard folders views
About styles, you were answered

V
Vladimir Soldatov, 2016-03-17
@TPbIHTPABA

there are also such crutches

list($path, $webPath) = Yii::$app->getAssetManager()->publish(__DIR__."/assets");
$this->getView()->registerJsFile($webPath."/js/login.js",['depends' => ['yii\bootstrap\BootstrapAsset']]);

But this is not the best option. In addition, during development, it is extremely inconvenient to delete assets every time. this way they are not updated when changed.
In general, yes, the best option is AppAsset.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question