- Gii – Generating Module
- Gii – Generating Controller
- Gii – Creating a Model
- Yii - Gii
- Yii - Localization
- Yii - Authorization
- Yii - Authentication
- Yii - Error Handling
- Yii - Logging
- Yii - Aliases
- Yii - Fragment Caching
- Yii - Caching
- Yii - Testing
- Yii - Fields
- Yii - RESTful APIs in Action
- Yii - RESTful APIs
- Yii - Theming
- Yii - Database Migration
- Yii - Active Record
- Yii - Query Builder
- Yii - Data Access Objects
- Yii - Database Access
- Yii - Dependency Injection
- Yii - Configurations
- Yii - Creating a Behavior
- Yii - Behaviors
- Yii - Creating Event
- Yii - Events
- Yii - GridView Widget
- Yii - ListView Widget
- Yii - Data Widgets
- Yii - Data Providers
- Yii - Properties
- Yii - Sorting
- Yii - Pagination
- Yii - Formatting
- Yii - Files Upload
- Yii - Using Cookies
- Yii - Cookies
- Yii - Using Flash Data
- Yii - Sessions
- Yii - AJAX Validation
- Yii - Ad Hoc Validation
- Yii - Validation
- Yii - HTML Forms
- Yii - Rules of URL
- Yii - URL Routing
- Yii - URL Formats
- Yii - Responses
- Yii - HTTP Requests
- Yii - Creating Extensions
- Yii - Extensions
- Yii - Asset Conversion
- Yii - Assets
- Yii - Layouts
- Yii - Views
- Yii - Modules
- Yii - Widgets
- Yii - Models
- Yii - Using Actions
- Yii - Using Controllers
- Yii - Controllers
- Yii - Entry Scripts
- Yii - Application Structure
- Yii - Create Page
- Yii - Installation
- Yii - Overview
- Yii - Home
Yii Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Yii - Using Actions
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.
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.
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.
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.
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.