Koʻp beriladigan savollar

Yii2 boʻyicha

  1. Yii2 da yangi theme qanday yaratiladi va u cookie ga qanday qo'shiladi

    1. frontend qismida themes papkasi yaratiladi.
      Sizda tahminan quyidagi tuzilish boʻladi: /frontend/themes/
      Ichiga kerakli theme nomi bilan papka yarating, masalan t516 va t415.
      Sizda quyidagi tuzilish boʻladi:
      frontend/
      ├── themes/
      │   ├── t516
      │   ├── t415
      
    2. Har bir theme papkasida views papkasi 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 uchun index.php faylida o'ziga xos kontent bo'lishi mumkin.
      Masalan, t516 theme uchun index.php faylida "Bu t516 theme" deb yozing, va t415 theme uchun "Bu t415 theme" deb yozing.
    3. Endi layout faylini yaratish va unga theme ni qo'shish kerak. Har bir theme papkasida layouts papkasi yarating va unga main.php faylini joylashtiring.
      Masalan:
      /frontend/themes/t516/layouts/main.php
      /frontend/themes/t415/layouts/main.php
      Natijada Sizda quyidagi tuzilish boʻladi:
      
      frontend/
      └── themes/
          ├── t516/
          │   ├── views/
          │   │   └── site/
          │   │       └── index.php
          │   └── layouts/
          │       └── main.php
          └── t415/
              ├── views/
              │   └── site/
              │       └── index.php
              └── layouts/
                  └── main.php
      
      Har bir main.php faylida o'ziga xos kontent bo'lishi mumkin. Masalan, t516 theme uchun main.php faylida
      <?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>
      
      va t415 theme uchun main.php faylida
      <?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>
      
    4. ThemeSelector component yaratamiz.
      Papka oching: /frontend/components/
      /frontend/components/ papkasida ThemeSelector.php faylini 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",
                  ],
              ]);
          }
      }
      
    5. Config faylga ThemeSelector component ni qo'shish.
      /frontend/config/main.php faylini oching va quyidagi o'zgarishlarni kiriting:
      'components' => [
          // ...
          'themeSelector' => [
              'class' => 'frontend\components\ThemeSelector',
          ],
          // ...
      ],
      
      va
      'bootstrap' => ['log', 'themeSelector'],
      qatorini qo'shing.
    6. Tekshirib koʻring.
      Endi brauzeringizda http://your.site.domain/site/index?theme=t516 manziliga o'ting va "Bu t516 theme" yozuvini ko'rishingiz kerak.