[英]Yii2 + redis class error
I have an error when i'm trying to configure Yii2 with Redis: 尝试使用Redis配置Yii2时出现错误:
Invalid Configuration – yii\\base\\InvalidConfigException 无效的配置– yii \\ base \\ InvalidConfigException
The configuration for the "db" component must contain a "class" element. “ db”组件的配置必须包含“ class”元素。
Yii2 and yii2-redis was installed with composer . 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
I just edit: project_dir/config/db.php 我只是编辑: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,
],
]
];
?>
You should add valid configuration for db
component for correct initialization: 您应该为
db
组件添加有效的配置,以进行正确的初始化:
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
...
];
...
],
];
Read more in official docs . 在官方文档中阅读更多内容。
Add the below lines in config/main.php 在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 和redis功能之后
yii::$app->cache->redis->hset('check',email,key);
'components' => [
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
]
],
],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.