- 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 - Modules
A module is an entity that has its own models, views, controllers, and possibly other modules. It is practically an apppcation inside the apppcation.
Step 1 − Create a folder called modules inside your project root. Inside the modules folder, create a folder named hello. This will be the basic folder for our Hello module.
Step 2 − Inside the hello folder, create a file Hello.php with the following code.
<?php namespace appmoduleshello; class Hello extends yiiaseModule { pubpc function init() { parent::init(); } } ?>
We have just created a module class. This should be located under the module s base path. Every time a module is accessed, an instance of the correspondent module class is created. The init() function is for initiapzing the module s properties.
Step 3 − Now, add two more directories inside the hello folder − controllers and views. Add a CustomController.php file to the controller’s folder.
<?php namespace appmoduleshellocontrollers; use yiiwebController; class CustomController extends Controller { pubpc function actionGreet() { return $this->render( greet ); } } ?>
When creating a module, a convention is to put the controller classes into the controller’s directory of the module s base path. We have just defined the actionGreet function, that just returns a greet view.
Views in the module should be put in the views folder of the module s base path. If views are rendered by a controller, they should be located in the folder corresponding to the controllerID. Add custom folder to the views folder.
Step 4 − Inside the custom directory, create a file called greet.php with the following code.
<h1>Hello world from custom module!</h1>
We have just created a View for our actionGreet. To use this newly created module, we should configure the apppcation. We should add our module to the modules property of the apppcation.
Step 5 − Modify the config/web.php file.
<?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 ), ], modules => [ hello => [ class => appmoduleshelloHello , ], ], 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; ?>
A route for a module s controller must begin with the module ID followed by the controller ID and action ID.
Step 6 − To run the actionGreet in our apppcation, we should use the following route.
hello/custom/greet
Where hello is a module ID, custom is a controller ID and greet is an action ID.
Step 7 − Now, type http://localhost:8080/index.php?r=hello/custom/greet and you will see the following output.
Important Points
Modules should −
Be used in large apppcations. You should spanide its features into several groups. Each feature group can be developed as a module.
Be reusable. Some commonly used features, as SEO management or blog management, can be developed as modules, so that you can easily reuse them in future projects.