Koʻp beriladigan savollar
Yii2 boʻyicha
-
Yii2 da yangi theme qanday yaratiladi va u cookie ga qanday qo'shiladi
-
frontendqismidathemespapkasi yaratiladi.
Sizda tahminan quyidagi tuzilish boʻladi:/frontend/themes/
Ichiga kerakli theme nomi bilan papka yarating, masalant516vat415.
Sizda quyidagi tuzilish boʻladi:frontend/ ├── themes/ │ ├── t516 │ ├── t415 -
Har bir theme papkasida
viewspapkasi yarating va unga kerakli view fayllarini joylashtiring.
Masalan:/frontend/themes/t516/views/site/index.php
/frontend/themes/t415/views/site/index.php
Har bir theme uchunindex.phpfaylida o'ziga xos kontent bo'lishi mumkin.
Masalan,t516theme uchunindex.phpfaylida "Bu t516 theme" deb yozing, vat415theme uchun "Bu t415 theme" deb yozing.
-
Endi layout faylini yaratish va unga theme ni qo'shish kerak. Har bir theme papkasida
layoutspapkasi yarating va ungamain.phpfaylini joylashtiring.
Masalan:/frontend/themes/t516/layouts/main.php
/frontend/themes/t415/layouts/main.php
Natijada Sizda quyidagi tuzilish boʻladi:
Har birfrontend/ └── themes/ ├── t516/ │ ├── views/ │ │ └── site/ │ │ └── index.php │ └── layouts/ │ └── main.php └── t415/ ├── views/ │ └── site/ │ └── index.php └── layouts/ └── main.phpmain.phpfaylida o'ziga xos kontent bo'lishi mumkin. Masalan,t516theme uchunmain.phpfaylida
va<?php /* @var $content string */ ?><!DOCTYPE html> <html lang="uz"> <head> <title>T516 theme main layout</title> </head> <body> <div style="padding:20px;"><?= $content ?></div> <hr> <a href="?theme=t516">t516</a> | <a href="?theme=t415">t415</a> </body> </html>t415theme uchunmain.phpfaylida
<?php /* @var $content string */ ?><!DOCTYPE html> <html lang="uz"> <head> <title>T415 theme main layout</title> </head> <body> <div style="padding:20px;"><?= $content ?></div> <hr> <a href="?theme=t516">t516</a> | <a href="?theme=t415">t415</a> </body> </html> -
ThemeSelector component yaratamiz.
Papka oching:/frontend/components/
/frontend/components/papkasidaThemeSelector.phpfaylini yaratib, unga quyidagi kodni joylashtiring:
<?php namespace frontend\components; use Yii; use yii\base\BootstrapInterface; use yii\base\Theme; class ThemeSelector implements BootstrapInterface { public function bootstrap($app) { $request = $app->request; // 1. URL dan theme olish $theme = $request->get('theme'); // 2. Ruxsat berilgan themelar $allowedThemes = ['t516', 't415']; if ($theme && in_array($theme, $allowedThemes)) { // cookie ga yozamiz Yii::$app->response->cookies->add(new \yii\web\Cookie([ 'name' => 'theme', 'value' => $theme, 'expire' => time() + 86400 * 30, ])); } else { // cookie dan olish (default t415) $theme = $request->cookies->getValue('theme', 't415'); } // 3. Theme ni o‘rnatish $app->view->theme = new Theme([ 'basePath' => "@frontend/themes/$theme", 'baseUrl' => "@web/themes/$theme", 'pathMap' => [ '@frontend/views' => "@frontend/themes/$theme/views", ], ]); } } -
Config faylga ThemeSelector component ni qo'shish.
/frontend/config/main.phpfaylini oching va quyidagi o'zgarishlarni kiriting:
va'components' => [ // ... 'themeSelector' => [ 'class' => 'frontend\components\ThemeSelector', ], // ... ],
qatorini qo'shing.'bootstrap' => ['log', 'themeSelector'],
-
Tekshirib koʻring.
Endi brauzeringizdahttp://your.site.domain/site/index?theme=t516manziliga o'ting va "Bu t516 theme" yozuvini ko'rishingiz kerak.
-