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 :
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 −
For instance −
If same action is reused in different places, then such actions are defined as standalone action.
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:
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:
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:
To fulfill the request, the controller will undergo the following lifecycle −
Some of the Important Points to be focused are:
The Controllers should −
Yii Related Interview Questions
|PHP Interview Questions||Zend Interview Questions|
|PHP and Jquery Interview Questions||PHP+MySQL Interview Questions|
|Sybase Interview Questions||Dbase Interview Questions|
|Magento Interview Questions||PHP5 Interview Questions|
|WordPress Interview Questions||CakePHP Interview Questions|
|CodeIgniter Interview Questions||Firebase Interview Questions|
|SQLite Interview Questions||PHP7 Interview Questions|
|Object Oriented Programming in PHP Interview Questions|
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.