当我尝试使用 Redis 配置 Yii2 时出现错误:
无效配置 – yii\base\InvalidConfigException
“db”组件的配置必须包含“class”元素。
Yii2 和 yii2-redis与 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
我只是编辑: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 .
关于php - Yii2 + redis类报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29338148/