Yii - 使用控制器



Web 應用中的控制器應該繼承自yii\web\Controller 或其子類。在控制檯應用程式中,它們應該繼承自yii\console\Controller 或其子類。

讓我們在controllers資料夾中建立一個示例控制器。

步驟 1 − 在Controllers資料夾中,建立一個名為ExampleController.php的檔案,其中包含以下程式碼。

<?php 
   namespace app\controllers; 
   use yii\web\Controller; 
   class ExampleController extends Controller { 
      public function actionIndex() { 
         $message = "index action of the ExampleController"; 
         return $this->render("example",[ 
            'message' => $message 
         ]); 
      } 
   } 
?>

步驟 2 − 在views/example資料夾中建立一個示例檢視。在該資料夾中,建立一個名為example.php的檔案,其中包含以下程式碼。

<?php 
   echo $message; 
?>

每個應用程式都有一個預設控制器。對於 Web 應用程式,該站點是控制器,而對於控制檯應用程式,它是 help。因此,當開啟https://:8080/index.php URL 時,站點控制器將處理請求。您可以在應用程式配置中更改預設控制器。

考慮以下程式碼:

'defaultRoute' => 'main'

步驟 3 − 將上述程式碼新增到config/web.php中。

<?php 
   $params = require(__DIR__ . '/params.php'); 
   $config = [ 
      'id' => 'basic', 
      'basePath' => dirname(__DIR__), 
      'bootstrap' => ['log'], 
      'components' => [ 
         'request' => [ 
            // !!! insert a secret key in the following (if it is empty) - this is
               //required by cookie validation 
            'cookieValidationKey' => 'ymoaYrebZHa8gURuolioHGlK8fLXCKjO', 
         ], 
         'cache' => [ 
            'class' => 'yii\caching\FileCache', 
         ], 
         'user' => [ 
            'identityClass' => 'app\models\User', 
            'enableAutoLogin' => true, 
         ], 
         'errorHandler' => [ 
            'errorAction' => 'site/error', 
         ], 
         'mailer' => [ 
            'class' => 'yii\swiftmailer\Mailer', 
            // send all mails to a file by default. You have to set 
            // 'useFileTransport' to false and configure a transport 
            // for the mailer to send real emails. 
            'useFileTransport' => true, 
         ], 
         'log' => [ 
            'traceLevel' => YII_DEBUG ? 3 : 0, 
            'targets' => [ 
               [ 
                  'class' => 'yii\log\FileTarget',
                  'levels' => ['error', 'warning'], 
               ], 
            ], 
         ], 
         'db' => require(__DIR__ . '/db.php'), 
      ], 
      //changing the default controller 
      'defaultRoute' => 'example', 
      'params' => $params, 
   ]; 
   if (YII_ENV_DEV) { 
      // configuration adjustments for 'dev' environment 
      $config['bootstrap'][] = 'debug'; 
      $config['modules']['debug'] = [ 
         'class' => 'yii\debug\Module', 
      ]; 
      $config['bootstrap'][] = 'gii'; 
      $config['modules']['gii'] = [ 
         'class' => 'yii\gii\Module', 
      ]; 
   } 
   return $config; 
?> 						  

步驟 4 − 在 Web 瀏覽器的位址列中輸入https://:8080/index.php,您將看到預設控制器是示例控制器。

Controller Example

注意 − 控制器 ID 應包含小寫英文字母、數字、正斜槓、連字元和下劃線。

要將控制器 ID 轉換為控制器類名,您應該執行以下操作:

  • 從用連字元分隔的所有單詞中取第一個字母並將其轉換為大寫。
  • 刪除連字元。
  • 將正斜槓替換為反斜槓。
  • 新增 Controller 字尾。
  • 新增控制器名稱空間。

示例

  • page 變成app\controllers\PageController

  • post-article 變成app\controllers\PostArticleController

  • user/post-article 變成app\controllers\user\PostArticleController

  • userBlogs/post-article 變成app\controllers\userBlogs\PostArticleController

廣告