Actions are defined in the controllers file. They need to be called while executing a request in an application through the URL.


Creating Action

An action is created by defining a public method whose name starts with the word action.

Example:

Step 1 We'll create an action named index2 in the SampleController.php file.

  1. <?php   
  2. namespace frontend\controllers;   
  3.   
  4. use Yii;   
  5. use yii\web\Controller;   
  6.   
  7. class SampleController extends Controller   
  8. {   
  9.     public function actionIndex()   
  10.     {   
  11.       return $this->render('index');   
  12.     }   
  13.       
  14.     public function actionIndex2()   
  15.     {   
  16.          return "<h1>This action is Index2</h2>";   
  17.     }   
  18. }   
  19. ?>  

Step 2 Run it on the browser.

http://localhost/action/frontend/web/index.php?r=sample/index2

YII Controllers action 1

Action IDs

An action is created to perform a particular request, and hence it is generally named as verbs like create, view, update, delete, etc.

An action ID can contain only these letters:

  • English letters in lower case
  • Underscores
  • Hyphens
  • Numbers after english alphabets like index2 in the above example.

Actions can be created in two ways:

  • Inline action
  • Standalone action

Inline Action

Inline action is a method in the controller class. These are the most commonly created actions when they don't need to be used more than once and are easy to create.

Inline action ID name is defined according to the following points:

  • Start the method name with the action prefix.
  • The first letter of the word after action will be in upper case.
  • Remove hyphens.

For example,

  • Index2 becomes actionIndex2
  • login-form becomes actionLoginForm

Standalone Action

A standalone action extends yii\base\Action or its child classes. These actions are mainly created when they need to be used in different controllers or redistributes as extensions.

They can be defined as separate classes and then connect them to your controllers. This way they will be able to reuse.

These actions must implement a method called run() and extend to yii\base\Action or a child class.

Example

We'll demonstrate a simple use of standalone action.

Step 1 Create a folder standing in the frontend directory of your Yii2 folder.

Step 2 Now create a MultiAction.php file in above created folder.

  1. <?php   
  2.    namespace frontend\standing;   
  3.    use yii\base\Action;   
  4.    class MultiAction extends Action {   
  5.       public function run() {   
  6.          return "<h3>This is StandAlone action example</h3>";   
  7.       }   
  8.    }   
  9. ?>  

Look at the above code, we have created a standalone action named as MultiAction which extends to Action class. The have implemented the run() method.

Step 3 In the above created SampleController.php file add some extra codes.

  1. <?php   
  2. namespace frontend\controllers;   
  3.   
  4. use Yii;   
  5. use yii\web\Controller;   
  6.   
  7. class SampleController extends Controller   
  8. {   
  9.      
  10.     public function actionIndex()   
  11.     {   
  12.       return $this->render('index');   
  13.     }   
  14.       
  15.      public function actions()  
  16.      {   
  17.          return [   
  18.             'multi' => 'frontend\standing\MultiAction',   
  19.          ];   
  20.       }   
  21.   
  22.   
  23.     public function actionIndex2()   
  24.     {   
  25.          return "<h1>This action is Index2</h2>";   
  26.     }   
  27. }   
  28. ?>  

Look at the above code, actions() method returns the standalone action which is multi action created in the standing folder.

Step 4 Run it on the browser with the URL,

http://localhost/action/frontend/web/index.php?r=sample/multi

YII Controllers action 2

Action Return Value

Return value of an action stands for the result of the corresponding action.

The following example shows an action being redirected to a new URL by returning a response object. The redirect() method always returns a response object.

Step1 Add the following code in the SampleController.php file.

  1. public function actionMysite()   
  2. {   
  3.     // redirect the user browser to http://example.com   
  4.     return $this->redirect('http://javatpoint.com');   
  5. }  

Step 2 Run it on the browser with the following URL,

http://localhost/action/frontend/web/index.php?r=sample/mysite

The above URL will result the javatpoint.com site in front of you.


Action Parameters

You can also add parameters to the action methods. Their values will be retrieved from the $_GET method using parameter name as key.

Step 1 Add the following code in the SampleController.php file.

  1. public function actionPara($a$b)   
  2.     {   
  3.         return "$a $b";   
  4.     }   

Step 2 Run it on the browser with the following URL,

http://localhost/action/frontend/web/index.php?r=sample/para&a=Welcome to&b=our site

YII Controllers action 3

In the above URL, if you'll not provide any value to a and b variables, exception error will be thrown.

Default Action

A default action is always specified in every controllers file. By default, it is set as index.

When in the route, URL only contains controller ID, then it goes to default action that is index. However this default value can be changed by overriding it.

namespace app\controllers;

use yii\web\Controller;

  1. class SiteController extends Controller  
  2. {  
  3.     public $defaultAction = 'main_page';  
  4.   
  5.     public function actionHome()  
  6.     {  
  7.         return $this->render('main_page');  
  8.     }  
  9. }