- 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 - Configurations
Configurations are used to create new objects or initiapzing the existing ones. Configurations usually include a class name and a pst of initial values. They may also include a pst of event handlers and behaviors.
The following is an example of the database configuration −
<?php $config = [ class => yiidbConnection , dsn => mysql:host = localhost;dbname = helloworld , username => vladimir , password => 12345 , charset => utf8 , ]; $db = Yii::createObject($config); ?>
The Yii::createObject() method takes a configuration array and creates an object based on the class named in the configuration.
The format of a configuration −
[ //a fully quapfied class name for the object being created class => ClassName , //initial values for the named property propertyName => propertyValue , //specifies what handlers should be attached to the object s events on eventName => $eventHandler, //specifies what behaviors should be attached to the object as behaviorName => $behaviorConfig, ]
The configuration file of a basic apppcation template is one of the most complex −
<?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 ], ], ], ], urlManager => [ // showScriptName => false, // enablePrettyUrl => true, // enableStrictParsing => true, // suffix => / ], 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; ?>
In the above configuration file, we do not define the class name. This is because we have already defined it in the index.php file −
<?php //defining global constans defined( YII_DEBUG ) or define( YII_DEBUG , true); defined( YII_ENV ) or define( YII_ENV , dev ); //register composer autoloader require(__DIR__ . /../vendor/autoload.php ); //include yii files require(__DIR__ . /../vendor/yiisoft/yii2/Yii.php ); //load apppcation config $config = require(__DIR__ . /../config/web.php ); //create, config, and process request (new yiiwebApppcation($config))->run(); ?>
Many widgets also use configurations as shown in the following code.
<?php NavBar::begin([ brandLabel => My Company , brandUrl => Yii::$app->homeUrl, options => [ class => navbar-inverse navbar-fixed-top , ], ]); echo Nav::widget([ options => [ class => navbar-nav navbar-right ], items => [ [ label => Home , url => [ /site/index ]], [ label => About , url => [ /site/about ]], [ label => Contact , url => [ /site/contact ]], Yii::$app->user->isGuest ? [ label => Login , url => [ /site/login ]] : [ label => Logout ( . Yii::$app->user->identity->username . ) , url => [ /site/logout ], pnkOptions => [ data-method => post ] ], ], ]); NavBar::end(); ?>
When a configuration is too complex, a common practice is to create a PHP file, which returns an array. Take a look at the config/console.php configuration file −
<?php Yii::setApas( @tests , dirname(__DIR__) . /tests ); $params = require(__DIR__ . /params.php ); $db = require(__DIR__ . /db.php ); return [ id => basic-console , basePath => dirname(__DIR__), bootstrap => [ log , gii ], controllerNamespace => appcommands , modules => [ gii => yiigiiModule , ], components => [ cache => [ class => yiicachingFileCache , ], log => [ targets => [ [ class => yiilogFileTarget , levels => [ error , warning ], ], ], ], db => $db, ], params => $params, ]; ?>
The default configurations can be specified by calpng the Yii::$container->set() method. It allows you to apply default configurations to all instances of the specified classes when they are called via the Yii::createObject() method.
For example, to customize the yiiwidgetsLinkPager class, so that all pnk pagers will show at most three buttons, you can use the following code.
Yii::$container->set( yiiwidgetsLinkPager , [ maxButtonCount => 3, ]);Advertisements