Answer the question
In order to leave comments, you need to log in
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',
],
],
<link href="<?php echo $this->theme->baseUrl ?>/css/style.css" rel="stylesheet" media="screen" title="default">
Answer the question
In order to leave comments, you need to log in
This is how it works in the first version. In the second version, Doka rules with scripts and stylesassets/AppAsset.php
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'],
],
],
],
class AppAsset extends AssetBundle
{
public $sourcePath = '@app/themes/mytheme';
public $css = [
'css/style.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
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;
}
}
'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']
]
]
],
],
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']]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question