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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:08:46  来源:igfitidea点击:

Laravel 4 : Call to undefined method Redis::connection()

laravellaravel-4redis

提问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-serveris running :

redis-server在跑 :

redis-server running

redis-server 正在运行

I can use the server on 127.0.0.1:6379:

我可以在以下位置使用服务器127.0.0.1:6379

enter image description here

在此处输入图片说明

2. php5-redis is installed

2.安装php5-redis

php5-redisis actually installed :

php5-redis实际安装:

enter image description here

在此处输入图片说明

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 connectionon 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 Redisinto 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

所以,你的系统有问题。要么你

  1. Have a custom class named Redissomewhere that's loaded before the aliases are setup

  2. Have Redisaliased to something other than a the Illuminate\Support\Facades\Redisfacade class

  3. You Redisfacade class has been modified to return a service identifier other than redis

  4. You've rebound the redisservice as some other class

  5. Per the comments below, you have the RedisPHP extension installed and the global extension class "wins"

  1. Redis在设置别名之前加载一个名为某处的自定义类

  2. Redis别名为Illuminate\Support\Facades\Redis外观类以外的其他东西

  3. 您的Redis外观类已被修改为返回除redis

  4. 您已将redis服务作为其他班级反弹

  5. 根据下面的评论,您已经Redis安装了PHP 扩展并且全局扩展类“获胜”

To find out where PHP thinks the Redisclass is, try

要找出 PHP 认为Redis类在哪里,请尝试

$r = new ReflectionClass('Redis');
var_dump($r->getClassFile());

To see if #4is 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.inifile adding

编辑php.ini文件添加

extension=php_redis.dll

Restart server and check phpinfo();. Now Redis should be there!

重新启动服务器并检查phpinfo();。现在 Redis 应该就在那里了!