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流覽器打開地址:http://localhost:8080/index.php?r=site/contact. 您將看到以下頁面輸出 -
Yii控制器

當您打開這個頁面,執行 SiteController 控制器的 contact 動作。代碼首先加載 ContactForm 模型。然後,它會傳遞模型進去並渲染 contact 視圖。


如果您填寫表格,然後點擊提交按鈕,將看到如下 -

注意,提交後這一次是執行以下代碼 -
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app>params ['adminEmail'])) {
   Yii::$app->session->setFlash('contactFormSubmitted');
   return $this->refresh();
}  

如果有一個POST請求,我們分配POST數據到模型,並嘗試發送電子郵件。如果成功的話,我們設置了快閃的消息並使用文本Thank you for contacting us. We will respond to you as soon as possible.並刷新頁面。

理解路由

在上面的例子中,在URL => http://localhost:8080/index.php?r=site/contact, 路由是 site/contact. 在SiteController 中的 contact 動作(actionContact)將被執行。

根由以下部分組成─
  • moduleID − 如果控制器屬於一個模組,則路由的範本ID這一部分會存在。

  • controllerID (在上面的例子的 site) − 唯一字串標識,在同一個模組或應用程式的所有控制器中的這個名稱是唯一的。

  • actionID (在上面的例子中的 contact) − 唯一字串標識,在同一個控制器中的所有動作名稱唯一(即類中的方法名稱)。

路由的格式是=>controllerID/actionID. 如果控制器屬於一個模組,那麼它具有以下格式:moduleID/controllerID/actionID.



上一篇: Yii入口腳本 下一篇: Yii使用控制器