- 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 Controllers
Controllers in web apppcations should extend from yiiwebController or its child classes. In console apppcations, they should extend from yiiconsoleController or its child classes.
Let us create an example controller in the controllers folder.
Step 1 − Inside the Controllers folder, create a file called ExampleController.php with the following code.
<?php namespace appcontrollers; use yiiwebController; class ExampleController extends Controller { pubpc function actionIndex() { $message = "index action of the ExampleController"; return $this->render("example",[ message => $message ]); } } ?>
Step 2 − Create an example view in the views/example folder. Inside that folder, create a file called example.php with the following code.
<?php echo $message; ?>
Each apppcation has a default controller. For web apppcations, the site is the controller, while for console apppcations it is help. Therefore, when the http://localhost:8080/index.php URL is opened, the site controller will handle the request. You can change the default controller in the apppcation configuration.
Consider the given code −
defaultRoute => main
Step 3 − Add the above code to the following config/web.php.
<?php $params = require(__DIR__ . /params.php ); $config = [ id => basic , basePath => dirname(__DIR__), bootstrap => [ log ], components => [ request => [ // !!! insert a secret key in the following (if it is empty) - this is //required by cookie vapdation cookieVapdationKey => ymoaYrebZHa8gURuopoHGlK8fLXCKjO , ], cache => [ class => yiicachingFileCache , ], user => [ identityClass => appmodelsUser , enableAutoLogin => true, ], errorHandler => [ errorAction => site/error , ], mailer => [ class => yiiswiftmailerMailer , // send all mails to a file by default. You have to set // useFileTransport to false and configure a transport // for the mailer to send real emails. useFileTransport => true, ], log => [ traceLevel => YII_DEBUG ? 3 : 0, targets => [ [ class => yiilogFileTarget , levels => [ error , warning ], ], ], ], db => require(__DIR__ . /db.php ), ], //changing the default controller defaultRoute => example , params => $params, ]; if (YII_ENV_DEV) { // configuration adjustments for dev environment $config[ bootstrap ][] = debug ; $config[ modules ][ debug ] = [ class => yiidebugModule , ]; $config[ bootstrap ][] = gii ; $config[ modules ][ gii ] = [ class => yiigiiModule , ]; } return $config; ?>
Step 4 − Type http://localhost:8080/index.php in the address bar of the web browser, you will see that the default controller is the example controller.
Note − The Controller IDs should contain Engpsh letters in lower case, digits, forward slashes, hyphens, and underscores.
To convert the controller ID to the controller class name, you should do the following −
Take the first letter from all words separated by hyphens and turn it into uppercase.
Remove hyphens.
Replace forward slashes with backward ones.
Add the Controller suffix.
Prepend the controller namespace.
Examples
page becomes appcontrollersPageController.
post-article becomes appcontrollersPostArticleController.
user/post-article becomes appcontrollersuserPostArticleController.
userBlogs/post-article becomes appcontrollersuserBlogsPostArticleController.