Laravel 4:调用未定义的方法 Redis::connection()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25975794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 : Call to undefined method Redis::connection()
提问by grena
I'm going crazy about this error. I've got a vagrant VM with Debian 7, generated with Puphpet, installation was fine.
我要为这个错误发疯了。我有一个带有 Debian 7 的流浪虚拟机,用 Puphpet 生成,安装很好。
1. Redis is installed and working
1. Redis 已安装并运行
redis-server
is running :
redis-server
在跑 :
I can use the server on 127.0.0.1:6379
:
我可以在以下位置使用服务器127.0.0.1:6379
:
2. php5-redis is installed
2.安装php5-redis
php5-redis
is actually installed :
php5-redis
实际安装:
3. Laravel Redis config is set
3. Laravel Redis 配置设置
Here is my redis config file in app/local/database.php
:
这是我的 redis 配置文件app/local/database.php
:
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],
4. The call to Redis is simple :
4.调用Redis很简单:
// Get redis
$redis = Redis::connection();
5. I tried a lot of things
5.我尝试了很多东西
sudo service nginx reload
sudo service redis-server force-reload
composer dumpautoload
But nothing solved the error.
但没有解决这个错误。
I'm still having :
我还有:
ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to undefined method Redis::connection()' in /var/www/fd/app/menus/admin.menu.php:16
(line 16 is where I do the connection $redis = Redis::connection();
)
(第 16 行是我进行连接的地方$redis = Redis::connection();
)
Where am I wrong ?
我哪里错了?
Btw, I hate mondays >.>
顺便说一句,我讨厌星期一>.>
回答by Neil Neyman
I came across this after encountering this issue and wanted to add another answer in case it helps someone else.
遇到这个问题后,我遇到了这个问题,并想添加另一个答案,以防它对其他人有帮助。
In my case there was an alias collision because my php configuration has the PHP-Redis module/extension enabled -- both the PHP module and Laravel seem to have a conflicting object named Redis. I was able to resolve this simply by using the entire namespaced identifier:
就我而言,存在别名冲突,因为我的 php 配置启用了 PHP-Redis 模块/扩展——PHP 模块和 Laravel 似乎都有一个名为 Redis 的冲突对象。我能够通过使用整个命名空间标识符来解决这个问题:
//$r = Redis::connection()
$r = Illuminate\Support\Facades\Redis::connection();
回答by Alan Storm
The problem isn't with your redis server setup -- there's something mis-configured or changed in your system.
问题不在于您的 redis 服务器设置——您的系统中配置错误或更改了某些内容。
The error you're seeing
你看到的错误
Call to undefined method Redis::connection()
Is PHP telling you it can't find a method named connection
on the class Redis
. It's a PHP error, and PHP never gets around to trying to talk to the redis server.
PHP 是否告诉您它找不到connection
在 class 上命名的方法Redis
。这是一个 PHP 错误,PHP 永远不会尝试与 redis 服务器通信。
Normally, in a Laravel 4.2 system, there is no class named Redis
. Instead, an alias is setup in app/config/app.php
通常,在 Laravel 4.2 系统中,没有名为Redis
. 相反,别名设置在app/config/app.php
#File: app/config/app.php
'Redis' => 'Illuminate\Support\Facades\Redis',
which turns Redis
into a facade. This is what enables you to make calls like Redis::connection
.
这变成Redis
了一个门面。这使您能够拨打类似Redis::connection
.
So, there's something wrong with your system. Either you
所以,你的系统有问题。要么你
Have a custom class named
Redis
somewhere that's loaded before the aliases are setupHave
Redis
aliased to something other than a theIlluminate\Support\Facades\Redis
facade classYou
Redis
facade class has been modified to return a service identifier other thanredis
You've rebound the
redis
service as some other classPer the comments below, you have the
Redis
PHP extension installed and the global extension class "wins"
Redis
在设置别名之前加载一个名为某处的自定义类已
Redis
别名为Illuminate\Support\Facades\Redis
外观类以外的其他东西您的
Redis
外观类已被修改为返回除redis
您已将
redis
服务作为其他班级反弹根据下面的评论,您已经
Redis
安装了PHP 扩展并且全局扩展类“获胜”
To find out where PHP thinks the Redis
class is, try
要找出 PHP 认为Redis
类在哪里,请尝试
$r = new ReflectionClass('Redis');
var_dump($r->getClassFile());
To see if #4
is the problem, try calling the service directly
查看是否#4
有问题,尝试直接调用服务
$app = app();
$app['redis']->connection();
Good luck!
祝你好运!
回答by Vagner do Carmo
That error is because you have installed and enabled the module php5-redis, it became with the class Redis. To avoid that error and use the Laravel Redis Facade, you have to change the alias in app/config/app.php (or whatever is your environment).
该错误是因为您已经安装并启用了模块 php5-redis,它变成了类 Redis。为了避免该错误并使用 Laravel Redis Facade,您必须更改 app/config/app.php(或任何您的环境)中的别名。
'Redis' => 'Illuminate\Support\Facades\Redis'
'Redis' => 'Illuminate\Support\Facades\Redis'
'RedisFacade' => 'Illuminate\Support\Facades\Redis' //whatever you like
'RedisFacade' => 'Illuminate\Support\Facades\Redis' //whatever you like
or just configure your cache.php to use Redis and use only the Cache class. :)
或者只是将您的 cache.php 配置为使用 Redis 并仅使用 Cache 类。:)
回答by Ezequiel Fernandez
Install Redis extension on your PC.
在您的 PC 上安装 Redis 扩展。
Download the CORRECT version the DDL from the following link: https://pecl.php.net/package/redis/4.1.0/windows
从以下链接下载正确版本的 DDL:https: //pecl.php.net/package/redis/4.1.0/windows
Put the dll in the correct folder
将dll放在正确的文件夹中
Wamp -> C:\wamp\bin\php\php-XXXX\ext
Laragon -> C:\laragon\bin\php\php-XXX\ext
Edit the php.ini
file adding
编辑php.ini
文件添加
extension=php_redis.dll
Restart server and check phpinfo();
. Now Redis should be there!
重新启动服务器并检查phpinfo();
。现在 Redis 应该就在那里了!