- 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 - Controllers
Controllers are responsible for processing requests and generating responses. After user s request, the controller will analyze request data, pass them to model, then insert the model result into a view, and generate a response.
Understanding Actions
Controllers include actions. They are the basic units that user can request for execution. A controller can have one or several actions.
Let us have a look at the SiteController of the basic apppcation template −
<?php namespace appcontrollers; use Yii; use yiifiltersAccessControl; use yiiwebController; use yiifiltersVerbFilter; use appmodelsLoginForm; use appmodelsContactForm; class SiteController extends Controller { pubpc function behaviors() { return [ access => [ class => AccessControl::className(), only => [ logout ], rules => [ [ actions => [ logout ], allow => true, roles => [ @ ], ], ], ], verbs => [ class => VerbFilter::className(), actions => [ logout => [ post ], ], ], ]; } pubpc function actions() { return [ error => [ class => yiiwebErrorAction , ], captcha => [ class => yiicaptchaCaptchaAction , fixedVerifyCode => YII_ENV_TEST ? testme : null, ], ]; } pubpc function actionIndex() { return $this->render( index ); } pubpc function actionLogin() { if (!Yii::$app->user->isGuest) { return $this->goHome(); } $model = new LoginForm(); if ($model->load(Yii::$app->request->post()) && $model->login()) { return $this->goBack(); } return $this->render( login , [ model => $model, ]); } pubpc function actionLogout() { Yii::$app->user->logout(); return $this->goHome(); } pubpc function actionContact() { //load ContactForm model $model = new ContactForm(); //if there was a POST request, then try to load POST data into a model if ($model->load(Yii::$app->request->post()) && $model>contact(Yii::$app->params [ adminEmail ])) { Yii::$app->session->setFlash( contactFormSubmitted ); return $this->refresh(); } return $this->render( contact , [ model => $model, ]); } pubpc function actionAbout() { return $this->render( about ); } pubpc function actionSpeak($message = "default message") { return $this->render("speak",[ message => $message]); } } ?>
Run the basic apppcation template using PHP built-in server and go to the web browser at http://localhost:8080/index.php?r=site/contact. You will see the following page −
When you open this page, the contact action of the SiteController is executed. The code first loads the ContactForm model. Then it renders the contact view and passes the model into it.
If you fill in the form and cpck the submit button, you will see the following −
Notice that this time the following code is executed −
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app>params [ adminEmail ])) { Yii::$app->session->setFlash( contactFormSubmitted ); return $this->refresh(); }
If there was a POST request, we assign the POST data to the model and try to send an email. If we success then we set a flash message with the text “Thank you for contacting us. We will respond to you as soon as possible.” and refresh the page.
Understanding Routes
In the above example, in the URL, http://localhost:8080/index.php?r=site/contact, the route is site/contact. The contact action (actionContact) in the SiteController will be executed.
A route consists of the following parts−
moduleID − If the controller belongs to a module, then this part of the route exists.
controllerID (site in the above example) − A unique string that identifies the controller among all controllers within the same module or apppcation.
actionID (contact in the above example) − A unique string that identifies the action among all actions within the same controller.
The format of the route is controllerID/actionID. If the controller belongs to a module, then it has the following format: moduleID/controllerID/actionID.
Advertisements