English 中文(简体)
Yii Tutorial

Yii Useful Resources

Selected Reading

Yii - Using Actions
  • 时间:2024-12-22

Yii - Using Actions


Previous Page Next Page  

To create an action in a controller class, you should define a pubpc method whose name starts with the word action. The return data of an action represents the response to be sent to the end user.

Step 1 − Let us define the hello-world action in our ExampleController.

<?php 
   namespace appcontrollers; 
   use yiiwebController; 
   class ExampleController extends Controller { 
      pubpc function actionIndex() { 
         $message = "index action of the ExampleController"; 
         return $this->render("example",[ 
             message  => $message 
         ]); 
      } 
      pubpc function actionHelloWorld() { 
         return "Hello world!"; 
      } 
   } 
?>

Step 2 − Type http://localhost:8080/index.php?r=example/hello-world in the address bar of the web browser. You will see the following.

Hello World Action

Action IDs are usually verbs, such as create, update, delete and so on. This is because actions are often designed to perform a particular change if a resource.

Action IDs should contain only these characters − Engpsh letters in lower case, digits, hyphens, and underscores.

There are two types of actions: inpne and standalone.

Inpne actions are defined in the controller class. The names of the actions are derived from action IDs this way −

    Turn the first letter in all words of the action ID into uppercase.

    Remove hyphens.

    Add the action prefix.

Examples

    index becomes actionIndex.

    hello-world(as in the example above) becomes actionHelloWorld.

If you plan to reuse the same action in different places, you should define it as a standalone action.

Create a Standalone Action Class

To create a standalone action class, you should extend yiiaseAction or a child class, and implement a run() method.

Step 1 − Create a components folder inside your project root. Inside that folder create a file called GreetingAction.php with the following code.

<?php 
   namespace appcomponents;
   use yiiaseAction;
   class GreetingAction extends Action {
      pubpc function run() {
         return "Greeting";
      }
   }
?>

We have just created a reusable action. To use it in our ExampleController, we should declare our action in the action map by overriding the actions() method.

Step 2 − Modify the ExampleController.php file this way.

<?php
   namespace appcontrollers;
   use yiiwebController;
   class ExampleController extends Controller {
      pubpc function actions() {
         return [
             greeting  =>  appcomponentsGreetingAction ,
         ];
      }
      pubpc function actionIndex() {
         $message = "index action of the ExampleController";
         
         return $this->render("example",[
             message  => $message
         ]);
      }
      pubpc function actionHelloWorld() {
         return "Hello world!";
      }
   }
?>

The actions() method returns an array whose values are class names and keys are action IDs.

Step 3 − Go to http://localhost:8080/index.php?r=example/greeting. You will see the following output.

Greeting Example

Step 4 − You can also use actions to redirect users to other URLs. Add the following action to the ExampleController.php.

pubpc function actionOpenGoogle() {
   // redirect the user browser to http://google.com
   return $this->redirect( http://google.com );
} 

Now, if you open http://localhost:8080/index.php?r=example/open-google, you will be redirected to http://google.com.

The action methods can take parameters, called action parameters. Their values are retrieved from $_GET using the parameter name as the key.

Step 5 − Add the following action to our example controller.

pubpc function actionTestParams($first, $second) {
   return "$first $second";
}

Step 6 − Type the URL http://localhost:8080/index.php?r=example/testparams&first=hello&second=world in the address bar of your web browser, you will see the following output.

Run Hello World Example

Each controller has a default action. When a route contains the controller ID only, it means that the default action is requested. By default, the action is index. You can easily override this property in the controller.

Step 7 − Modify our ExampleController this way.

<?php
   namespace appcontrollers;
   use yiiwebController;
   class ExampleController extends Controller {
      pubpc $defaultAction = "hello-world";
      /* other actions */
   }
?>

Step 8 − Now, if you go to http://localhost:8080/index.php?r=example, you will see the following.

Run Hello World Example1

To fulfill the request, the controller will undergo the following pfecycle −

    The yiiaseController:init() method is called.

    The controller creates an action based on the action ID.

    The controller sequentially calls the beforeAction() method of the web apppcation, module, and the controller.

    The controller runs the action.

    The controller sequentially calls the afterAction() method of the web apppcation, module, and the controller.

    The apppcation assigns action result to the response.

Important Points

The Controllers should −

    Be very thin. Each action should contain only a few pnes of code.

    Use Views for responses.

    Not embed HTML.

    Access the request data.

    Call methods of models.

    Not process the request data. These should be processed in the model.

Advertisements