Yii2 + redis class 错误

Yii2 + redis class error

我在尝试使用 Redis 配置 Yii2 时遇到错误:

无效配置 – yii\base\InvalidConfigException
"db" 组件的配置必须包含 "class" 元素。

Yii2 yii2-rediscomposer.

一起安装
# 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

我刚刚编辑: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,
               ],
                    ]
            ];
?>

您应该为 db 组件添加有效配置以正确初始化:

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

            ...
        ];

        ...
    ],
];

official docs 阅读更多内容。

在config/main中添加以下行。php

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

要使用它,您必须按以下方式使用它

yii::$app->cache->redis

之后的redis功能如

yii::$app->cache->redis->hset('check',email,key);
'components' => [
    'cache' => [
        'class' => 'yii\redis\Cache',
        'redis' => [
                'hostname' => 'localhost',
                'port' => 6379,
                'database' => 0,
        ]

    ],
],