Controllers Action
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.
Step 2 Run it on the browser.
http://localhost/action/frontend/web/index.php?r=sample/index2
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 yiibaseAction 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 yiibaseAction 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.
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.
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
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.
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 tutoraspire.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.
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
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 appcontrollers;
use yiiwebController;