1

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,
               ],
                    ]
            ];
?>

3 Answers 3

1

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

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

            ...
        ];

        ...
    ],
];

Read more in official docs.

Sign up to request clarification or add additional context in comments.

1 Comment

any clue for The configuration for the "cache" component must contain a "class" element. i configured the cache class like this 'class' => 'yii\redis\Cache'
1

Add the below lines in config/main.php

    'cache' => [
        'class' => 'yii\redis\Cache',
        'redis' => [
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0,
        ]
    ],

and for using that you have to use it in the following way

yii::$app->cache->redis

and the redis functions after that like

yii::$app->cache->redis->hset('check',email,key);

Comments

0
'components' => [
    'cache' => [
        'class' => 'yii\redis\Cache',
        'redis' => [
                'hostname' => 'localhost',
                'port' => 6379,
                'database' => 0,
        ]

    ],
],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.