May be it will be stupid question, but still…Could anybody explain me some basic syntax for including components in configuration file? I can’t understand why components including doesn’t have unified syntax like in hypothetical example below.
The Yii2 configuration code block:
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mail' => [
'class' => 'yii\swiftmailer\Mailer',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
],
The expected form:
'components' => [
'UrlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
],
'FileCache' => [
'class' => 'yii\caching\FileCache',
],
'User' => [
'class' => 'yii\path\to\User', #?
'identityClass' => 'app\models\User',
],
'ErrorHandler' => [
'class' => 'yii\path\to\ErrorHandler',
'errorAction' => 'site/error',
],
'Mailer' => [
'class' => 'yii\swiftmailer\Mailer',
],
'log' => [
'class' => 'yii\log\FileTarget', #?
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'levels' => ['error', 'warning'],
],
],
],
],
As you can see, the main changes are:
-
Component name and class name match - I think is logical and easy to remember.
-
Each component has class key value pair at the top level of component’s array.
-
Subsequent key value pairs are components options.
I find it hard to learn when there is weak logical link and many aliases for component’s names.
Sorry for the mistakes I’ve made. I’m not English man.