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。您将看到以下页面 −

运行基本应用程序

打开此页面时,将执行 SiteController 的联系操作。代码首先加载 ContactForm 模型。然后它渲染联系人视图并将模型传递到其中。

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 数据分配给模型并尝试发送电子邮件。如果成功,我们将设置一条带有文本"感谢您联系我们。我们将尽快回复您。"的 flash 消息并刷新页面。

了解路由

在上面的示例中,在 URL http://localhost:8080/index.php?r=site/contact 中,路由为 site/contact。将执行 SiteController 中的联系操作 (actionContact)。

路由由以下部分组成−

  • moduleID −如果控制器属于某个模块,则存在此部分路由。

  • controllerID(上例中的 site)− 在同一模块或应用程序内的所有控制器中标识控制器的唯一字符串。

  • actionID(上例中的 contact)− 在同一控制器内的所有操作中标识操作的唯一字符串。

路由的格式为 controllerID/actionID。如果控制器属于某个模块,则其格式为:moduleID/controllerID/actionID