Yii - 控制器



控制器負責處理請求並生成響應。在使用者請求後,控制器將分析請求資料,將其傳遞給模型,然後將模型結果插入檢視,並生成響應。

理解操作

控制器包含操作。它們是使用者可以請求執行的基本單元。一個控制器可以有一個或多個操作。

讓我們看一下基本應用程式模板的SiteController

<?php 
   namespace app\controllers; 
   use Yii; 
   use yii\filters\AccessControl; 
   use yii\web\Controller; 
   use yii\filters\VerbFilter; 
   use app\models\LoginForm; 
   use app\models\ContactForm; 
   class SiteController extends Controller { 
      public function behaviors() { 
         return [ 
            'access' => [ 
               'class' => AccessControl::className(), 
               'only' => ['logout'], 
               'rules' => [ 
                  [ 
                     'actions' => ['logout'], 
                     'allow' => true, 
                     'roles' => ['@'], 
                  ], 
               ], 
            ], 
            'verbs' => [
               'class' => VerbFilter::className(), 
               'actions' => [ 
                  'logout' => ['post'], 
               ], 
            ], 
         ]; 
      } 
      public function actions() { 
         return [ 
            'error' => [ 
               'class' => 'yii\web\ErrorAction', 
            ], 
            'captcha' => [ 
               'class' => 'yii\captcha\CaptchaAction', 
               'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, 
            ], 
         ]; 
      } 
      public function actionIndex() { 
         return $this->render('index'); 
      } 
      public function actionLogin() { 
         if (!\Yii::$app->user->isGuest) { 
            return $this->goHome(); 
         } 
         $model = new LoginForm(); 
         if ($model->load(Yii::$app->request->post()) && $model->login()) { 
            return $this->goBack(); 
         } 
         return $this->render('login', [ 
            'model' => $model, 
         ]); 
      }
      public function actionLogout() { 
         Yii::$app->user->logout();  
         return $this->goHome(); 
      } 
      public function actionContact() { 
         //load ContactForm model 
         $model = new ContactForm(); 
         //if there was a POST request, then try to load POST data into a model 
         if ($model->load(Yii::$app->request->post()) && $model>contact(Yii::$app->params
            ['adminEmail'])) { 
            Yii::$app->session->setFlash('contactFormSubmitted');  
            return $this->refresh(); 
         } 
         return $this->render('contact', [ 
            'model' => $model, 
         ]); 
      } 
      public function actionAbout() { 
         return $this->render('about'); 
      } 
      public function actionSpeak($message = "default message") { 
         return $this->render("speak",['message' => $message]); 
      } 
   } 
?>

使用 PHP 內建伺服器執行基本應用程式模板,並在 Web 瀏覽器中訪問https://:8080/index.php?r=site/contact。您將看到以下頁面:

Run Basic Application

當您開啟此頁面時,將執行SiteController的contact操作。程式碼首先載入ContactForm模型。然後它呈現contact檢視並將模型傳遞給它。

Contact Form Model

如果您填寫表單並單擊提交按鈕,您將看到以下內容:

Submit Form

請注意,這次執行了以下程式碼:

if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app>params ['adminEmail'])) { 
   Yii::$app->session->setFlash('contactFormSubmitted'); 
   return $this->refresh(); 
} 

如果有POST請求,我們將POST資料分配給模型並嘗試傳送電子郵件。如果成功,我們將設定一條帶有文字“感謝您聯絡我們。我們會盡快回復您。”的快閃記憶體訊息,並重新整理頁面。

理解路由

在上面的示例中,在URLhttps://:8080/index.php?r=site/contact中,路由是site/contact。將執行SiteController中的contact操作(actionContact)。

路由由以下部分組成:

  • moduleID - 如果控制器屬於某個模組,則路由的這部分存在。

  • controllerID(如上例中的site)- 一個唯一字串,用於在同一模組或應用程式中的所有控制器中標識控制器。

  • actionID(如上例中的contact)- 一個唯一字串,用於在同一控制器中的所有操作中標識操作。

路由的格式為controllerID/actionID。如果控制器屬於某個模組,則其格式如下:moduleID/controllerID/actionID

廣告