Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Entry script and configuration

Every request to the site arrives at one file, the entry script web/index.php. It reads the settings from the config folder and starts the application. You need to know these two places to understand where each feature is switched on.

The entry script

The file web/index.php already exists in the project:

<?php
// web/index.php
 
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
 
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
 
$config = require __DIR__ . '/../config/web.php';
 
(new yii\web\Application($config))->run();

Two constants switch on development mode, then the libraries are loaded, the settings array is read and the application object is created. The run() method finds the right controller and sends the response.

The configuration file

The site settings are in config/web.php. It is a plain PHP file that returns an array:

// config/web.php
$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'name' => 'My blog',
    'language' => 'en-US',
    'components' => [
        // application services
    ],
    'params' => $params,
];
 
return $config;

The name key sets the site name, which the standard layout shows in the header. The language key sets the language of framework messages, for example form errors.

Components

A component is a named application service: the database, the cache, the mailer. Components are listed in the components section:

// config/web.php
'components' => [
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ],
    'db' => $db,
],

The array key is the component name, class is its class, and the other keys are properties of that class. A component is reached through the application object Yii::$app by its name:

Yii::$app->cache;     // the cache
Yii::$app->db;        // the database connection
Yii::$app->request;   // the current request
Yii::$app->user;      // the current user

A component object is created on first access, so unused components do not slow the site down.

Your own parameters

Values needed in different places of the site go into config/params.php:

// config/params.php
return [
    'adminEmail' => 'admin@example.com',
    'postsPerPage' => 10,
];

Anywhere in the code a parameter is read like this: Yii::$app->params['postsPerPage'].

Other configuration files

FilePurpose
config/web.phpSite settings
config/console.phpSettings of console commands
config/db.phpThe database connection
config/params.phpYour own parameters

Common mistake

The site and the console have separate settings. A component added only to config/web.php is not available in a console command: accessing it ends with the Unknown component ID error. If a service is needed in both places, describe it in config/console.php as well.

Thanks for reading!

Donate to the site — we work hard for you.