11

I'm going crazy about this error. I've got a vagrant VM with Debian 7, generated with Puphpet, installation was fine.

1. Redis is installed and working

redis-server is running :

redis-server running

I can use the server on 127.0.0.1:6379 :

enter image description here

2. php5-redis is installed

php5-redis is actually installed :

enter image description here

3. Laravel Redis config is set

Here is my redis config file in app/local/database.php :

'redis' => [

    'cluster' => false,

    'default' => [
    'host'     => '127.0.0.1',
    'port'     => 6379,
    'database' => 0,
    ],
],

4. The call to Redis is simple :

// Get redis
$redis = Redis::connection();

5. I tried a lot of things

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();)

Where am I wrong ?

Btw, I hate mondays >.>

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
grena
  • 1,011
  • 1
  • 10
  • 25

4 Answers4

22

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:

//$r = Redis::connection() 
$r = Illuminate\Support\Facades\Redis::connection();
Neil Neyman
  • 2,116
  • 16
  • 21
  • Thank you for your solution. In fact, my vagrant machine had php-redis installed too, so in my case it was the correct answer ;) – grena Oct 20 '14 at 00:13
  • 2
    Or maybe you can just add additional entry under app.php > aliases with the same value. Example: ``'Redis2' => 'Illuminate\Support\Facades\Redis',`` Then do ``php artisan dump-autoload`` – lukaserat Dec 04 '14 at 05:41
  • 1
    @lukaserat thanks you, this is what [Laravel's docs suggest](http://laravel.com/docs/4.2/redis#introduction), but I didn't figure out exactly what they meant by: `Note: If you have the Redis PHP extension installed via PECL, you will need to rename the alias for Redis in your app/config/app.php file.` Apparently that means: 1. rename the alias in `app/config/app.php` from `'Redis' => 'Illuminate\Support\Facades\Redis',` to, i.e., `'LRedis' => ...` 2. issue `artisan dump-autoload` command 3. use that renamed alias in your code: `$r = LRedis::connection();` – Serge Dec 05 '14 at 09:15
  • Thanks for saving my time ! – Anand Mishra Jul 09 '15 at 08:43
6

The problem isn't with your redis server setup -- there's something mis-configured or changed in your system.

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.

Normally, in a Laravel 4.2 system, there is no class named Redis. Instead, an alias is setup in 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.

So, there's something wrong with your system. Either you

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

  2. Have Redis aliased to something other than a the Illuminate\Support\Facades\Redis facade class

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

  4. You've rebound the redis service as some other class

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

To find out where PHP thinks the Redis class is, try

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

To see if #4 is the problem, try calling the service directly

$app = app();
$app['redis']->connection();

Good luck!

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • Replacing all `Redis::connection()` with `$app['redis']->connection()` worked. I must have another error somewhere else, if I find it, I'll post it here in comments, thank you ! – grena Sep 29 '14 at 11:43
  • 2
    I ran into this problem recently with an installation of PHP 5.6 that happened to be running the php-redis module -- I came across this post as well as a Laravel github issue here: https://github.com/laravel/framework/issues/1066 . In my case, and as it appears to happen on occasion, the Laravel Redis alias is colliding with the Redis PHP module. Seems the best solution is to find the alias definition in Laravel and rename it to something like LRedis – Neil Neyman Oct 18 '14 at 00:06
  • +1 & tks for the reflection technique, which I'm sure will be extremely useful in a lot of scenarios and not just this. – techfoobar Feb 24 '15 at 11:14
2

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).

'Redis' => 'Illuminate\Support\Facades\Redis'

'RedisFacade' => 'Illuminate\Support\Facades\Redis' //whatever you like

or just configure your cache.php to use Redis and use only the Cache class. :)

Vagner do Carmo
  • 113
  • 1
  • 4
1

Install Redis extension on your PC.

Download the CORRECT version the DDL from the following link: https://pecl.php.net/package/redis/4.1.0/windows

Put the dll in the correct folder

Wamp -> C:\wamp\bin\php\php-XXXX\ext
Laragon -> C:\laragon\bin\php\php-XXX\ext

Edit the php.ini file adding

extension=php_redis.dll

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

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Ezequiel Fernandez
  • 954
  • 11
  • 18