Yii Using Actions - Yii

How to create Yii Actions?

An action is created in a controller class by defining public method, whose name starts with the action word. The action’s return data corresponds to the response to be sent to the user.

Yii actions are created by the following steps:

Step 1 − Define the hello-world action in ExampleController.

Step 2 − Enter http://localhost:8080/index.php?r=example/hello-world in the address bar of the web browser and the screen appears as :

Yii Hello World Action

Action IDs are mostly verbs like create, update, delete etc, since actions are designed to execute a particular change in a resource.

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

Actions are of two types - Inline and standalone.

Inline actions are defined in the controller class. The names of the actions are derived from action IDs by −

  • Changing the first letter in all words of the action ID into uppercase.
  • Removing hyphens.
  • Adding the action prefix.

For instance −

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

If same action is reused in different places, then such actions are defined as standalone action.

How to create a Standalone Action Class?

A standalone action class is created by extending yiiaseAction or a child class, and implement a run() method.

Step 1 – In the project root, create a components folder , and create a file GreetingAction.php in the folder created by the code:

By the code a reusable action is created. To use it in ExampleController, action is to be declared in the action map by overriding the actions() method.

Step 2 − Modify the ExampleController.php by :

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 and the output appears as:

Yii Greeting Example

Step 4 − Actions can be used to redirect users to other URLs. To the ExampleController.php.add the following action:

Now, open http://localhost:8080/index.php?r=example/open-google, and the page 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 – To the example controller add the following action:

Step 6 – Enter the URL http://localhost:8080/index.php?r=example/testparams&first=hello&second=world in the address bar of your web browser, and the output appears as:

Yii Run Hello World Example

Each controller has a default action. A default action is requested, when a route contains only the controller ID and the action is index by default. This property in the controller can be easily overridden.

Step 7 − Modify the ExampleController by the code:

Step 8 − Go to http://localhost:8080/index.php?r=example, and the output appears as:

Yii Run Hello World Example 2

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

  • 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 application, module, and the controller.
  • The controller runs the action.
  • The controller sequentially calls the afterAction()method of the web application, module, and the controller.
  • The application assigns action result to the response.

What are the important points to be noted while using Actions in Yii?

Some of the Important Points to be focused are:

The Controllers should −

  • Be very thin. Each action should contain only a few lines 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.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Yii Topics