laravel5.*安装使用Redis以及解决Class 'Predis\Client' not found和Fatal error: Non-static method Redis::set() cannot be called statically错误

https://phpartisan.cn/news/35.html

laravel中我们可以很简单的使用Redis,如何在服务器安装Redis以及原创访问你们可以访问Ubuntu 设置Redis密码以及允许远程访问和在ubuntu 安装redis的简单方法就可以了,很多人在使用的时候常常出现Class 'Predis\Client' not found的错误,那是因为我们laravel没有安装Redis拓展包,好的,我们接下来安装使用Redis

composer.jsonrequire里输入

"predis/predis": "^1.0"

然后更新一下

composer update

好的我们接下来在.env里配置一下我们的Redis路径

REDIS_HOST=你的IP
REDIS_PASSWORD=密码
REDIS_PORT=6379

好的,配置好了,接下来我们就是使用了

Redis::set('name', 'Taylor');
$test =  Redis::get('name');
dd($test);

如果没问题,应该会输出"Taylor",如果出现如下报错,代表你的Redis链接错了

Connection refused [tcp://123.1.1.1:6379]

好的,现在我们就搞定了Redis的安装和使用

Redis扩展冲突导致:Fatal error: Non-static method Redis::keys() cannot be called statically

如果你是通过 PECL 安装 Redis PHP 扩展,则需要重命名 config/app.php 文件里的 Redis 别名。

方法1、在每个使用use Redis;的文件中,我们改为use Illuminate\Support\Facades\Redis;即可
方法2、在config/app.php中的aliases修改Redis修改为Redisaa这种别名,然后use Redisaa;即可

猜你喜欢

转载自www.cnblogs.com/lxwphp/p/9172675.html