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使用控制器