English 中文(简体)
Yii Tutorial

Yii Useful Resources

Selected Reading

Yii - Controllers
  • 时间:2024-09-17

Yii - Controllers


Previous Page Next Page  

Controllers are responsible for processing requests and generating responses. After user s request, the controller will analyze request data, pass them to model, then insert the model result into a view, and generate a response.

Understanding Actions

Controllers include actions. They are the basic units that user can request for execution. A controller can have one or several actions.

Let us have a look at the SiteController of the basic apppcation template −

<?php 
   namespace appcontrollers; 
   use Yii; 
   use yiifiltersAccessControl; 
   use yiiwebController; 
   use yiifiltersVerbFilter; 
   use appmodelsLoginForm; 
   use appmodelsContactForm; 
   class SiteController extends Controller { 
      pubpc function behaviors() { 
         return [ 
             access  => [ 
                class  => AccessControl::className(), 
                only  => [ logout ], 
                rules  => [ 
                  [ 
                      actions  => [ logout ], 
                      allow  => true, 
                      roles  => [ @ ], 
                  ], 
               ], 
            ], 
             verbs  => [
                class  => VerbFilter::className(), 
                actions  => [ 
                   logout  => [ post ], 
               ], 
            ], 
         ]; 
      } 
      pubpc function actions() { 
         return [ 
             error  => [ 
                class  =>  yiiwebErrorAction , 
            ], 
             captcha  => [ 
                class  =>  yiicaptchaCaptchaAction , 
                fixedVerifyCode  => YII_ENV_TEST ?  testme  : null, 
            ], 
         ]; 
      } 
      pubpc function actionIndex() { 
         return $this->render( index ); 
      } 
      pubpc 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, 
         ]); 
      }
      pubpc function actionLogout() { 
         Yii::$app->user->logout();  
         return $this->goHome(); 
      } 
      pubpc 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, 
         ]); 
      } 
      pubpc function actionAbout() { 
         return $this->render( about ); 
      } 
      pubpc function actionSpeak($message = "default message") { 
         return $this->render("speak",[ message  => $message]); 
      } 
   } 
?>

Run the basic apppcation template using PHP built-in server and go to the web browser at http://localhost:8080/index.php?r=site/contact. You will see the following page −

Run Basic Apppcation

When you open this page, the contact action of the SiteController is executed. The code first loads the ContactForm model. Then it renders the contact view and passes the model into it.

Contact Form Model

If you fill in the form and cpck the submit button, you will see the following −

Submit Form

Notice that this time the following code is executed −

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

If there was a POST request, we assign the POST data to the model and try to send an email. If we success then we set a flash message with the text “Thank you for contacting us. We will respond to you as soon as possible.” and refresh the page.

Understanding Routes

In the above example, in the URL, http://localhost:8080/index.php?r=site/contact, the route is site/contact. The contact action (actionContact) in the SiteController will be executed.

A route consists of the following parts−

    moduleID − If the controller belongs to a module, then this part of the route exists.

    controllerID (site in the above example) − A unique string that identifies the controller among all controllers within the same module or apppcation.

    actionID (contact in the above example) − A unique string that identifies the action among all actions within the same controller.

The format of the route is controllerID/actionID. If the controller belongs to a module, then it has the following format: moduleID/controllerID/actionID.

Advertisements