Morz Morz - 4 months ago 39
PHP Question

Yii2 + redis class error

I have an error when i'm trying to configure Yii2 with Redis:

Invalid Configuration – yii\base\InvalidConfigException

The configuration for the "db" component must contain a "class" element.

Yii2
and yii2-redis was installed with composer.

# ls -la project_dir/vendor/yiisoft/yii2-redis/
total 116
drwxr-xr-x 2 root root 4096 мар 26 13:59 .
drwxr-xr-x 11 root root 4096 мар 25 14:54 ..
-rw-r--r-- 1 root root 18013 мар 1 14:22 ActiveQuery.php
-rw-r--r-- 1 root root 11140 мар 1 14:22 ActiveRecord.php
-rw-r--r-- 1 root root 2194 мар 1 14:22 CHANGELOG.md
-rw-r--r-- 1 root root 6390 мар 1 14:22 Cache.php
-rw-r--r-- 1 root root 22224 мар 26 13:59 Connection.php
-rw-r--r-- 1 root root 1622 мар 1 14:22 LICENSE.md
-rw-r--r-- 1 root root 14015 мар 1 14:22 LuaScriptBuilder.php
-rw-r--r-- 1 root root 5650 мар 1 14:22 README.md
-rw-r--r-- 1 root root 5170 мар 1 14:22 Session.php
-rw-r--r-- 1 root root 891 мар 1 14:22 composer.json


I just edit: project_dir/config/db.php

<?php
/*return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2basic',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
];*/
return [
'components' => [
'redis' => [
'class' => 'yii\redis\Connection', tied to replace with "'class' => 'yii2-redis\redis\Connection',"
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
]
];
?>

Answer

You should add valid configuration for db component for correct initialization:

return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',

            ...
        ];

        ...
    ],
];

Read more in official docs.