Framework: Call to undefined method Redis::connection()

Created on 24 Apr 2013  ·  40Comments  ·  Source: laravel/framework

I believed I followed the steps really well in the docs (http://four.laravel.com/docs/redis) but I still get a fatal error exception thrown.

I have now set up a cache driver instead, all is well and working - but I guess the docs are wrong? ..or do I miss interpret everything again? :)

Most helpful comment

use IlluminateSupportFacadesRedis;

All 40 comments

Do you have "cluster" set to "false" in the Redis configuration? Also, can you paste the entire error?

The cluster option is not set what so ever..

I have now tried - still the same error.
The following are my settings:

'redis' => array(  
  // also tried false - no luck
  'cluster' => true, 

  // my private settings not relevant to the question at hand
  'default' => array(
    'host'     => '...',
    'port'     => '...',
    'database' => '...',
  ),

The entire error is in the title: Error: Call to undefined method Redis::connection() in.. line ..

Stacktrace _- though it shouldn't offer to much information in this case, I believe_

Symfony\Component\Debug\Exception\FatalErrorException
…/­app/­controllers/­CmsController.php16
Symfony\Component\Debug\ErrorHandler handleFatal
…/­bootstrap/­compiled.php5006
Illuminate\Exception\ExceptionServiceProvider Illuminate\Exception\{closure}
<#unknown>0

Hey erik,

Can you tell more about your stack? i.e os, wamp, lamp, etc...
Also have you checked Redis server is working and redis client is able to connect with it ? Some times redis server is crashed or closed unexpectedly then you may have to restart or shutdown them and work again.

If you're running in a *nix environment, you can check the netstat output to see if Redis is listening on whatever port you have it configured to listen on:

netstat -na | grep 6379

You should see output like this if it's listening:

tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:53760 127.0.0.1:6379 TIME_WAIT
tcp 0 0 127.0.0.1:6379 127.0.0.1:48107 ESTABLISHED
tcp 0 0 127.0.0.1:53758 127.0.0.1:6379 TIME_WAIT
tcp 0 0 127.0.0.1:48107 127.0.0.1:6379 ESTABLISHED

As I stated in the first post, it's working with a driver for the Cache facade, e.g: Cache::put('foo', 'bar', 10)
Also there are huge traffic on thees servers with sites running up to a mill visitors a day, on a good day that is. If there was anything wrong with redis we should notice it pretty quick :)

I've also tried to connect to the dev server with the same result before I found the Cache -solution..

@hardikdangar The os is FreeBSD .. we are running a few other stuff on that server - but nothing that should be of any relevance..
The dev server, where the PHP code is running, is using FreeBSD - Apache - PHP 5.4.13

It's working find on my end. Just curious, do you have phpredis PHP extension installed?

It's also working on my end on Ubuntu 12.04 and PHP 5.4.14 with a localhost instance of Redis server.

Here's the redis config in app/config/database.php on my dev server:

    'redis' => array(

            'cluster' => false,

            'default' => array(
                    'host'     => '127.0.0.1',
                    'port'     => 6379,
                    'database' => 0,
            ),
    ),

Then, in app/config/cache.php, I have the driver set to redis:

'driver' => 'redis',

I'm also using redis as the driver in app/config/session.php:

'driver' => 'redis',

As a test, I put this in my main Home controller:

            $redis = Redis::connection();
            $redis->set('name', 'Dave');
            $name = $redis->get('name');

I var_dumped $name and sure enough it was set to 'Dave'.

I keep Laravel up-to-date though with the latest from git and composer updates. If you're set up to fetch from the laravel git repo, I'd say give that a try.

git fetch upstream
git merge upstream/develop (or however you have it set up)
php composer.phar update

To get set up like that, I followed the excellent article: http://niallobrien.me/2013/03/installing-and-updating-laravel-4/

I will look some more on this tomorrow when I get back to work, I belive I ran composer update earlyer today though. In the mean time.. What's the difference between

$redis = Redis::connection();
$redis->set('name', 'Dave');
$name = $redis->get('name');

And

Cache::forever('name', 'Daver');
Cache::get('name');

If the cache driver is set to 'redis that is? Sense one of them is working for me and the other doesn't, the solution (or problem) should be in the difference.

In app/config/application.php, how is Redis defined in the list of aliases?

The difference with Cache is that the Facade is not used there.

Yeah, as of right now, my main hunch is that you have the phpredis PHP extension installed, which has a Redis class, and Laravel's "Redis" facade is conflicting with it. We really need to rename that facade if I can come up with a decent name.

@taylorotwell

I thought if we namespace it, it shouldn't conflict or is it ?

I guess it's already namespaced, but the alias allows for direct usage. what if you add the following at the top of your source:

use Illuminate\Redis\Database as Redis;

If that doesn't work (I don't know how PHP handles overriding an existing class with a use statement) import it under another name (or alter the alias name in app/config/application.php.

@JoostK I tried your example with no success
@taylorotwell It seems as if that extension is indeed installed. I'm not the server technician so I don't have full access, but what I can see from php -i | grep redis and phpinfo(INFO_MODULES) it looks to be the case..

I have recreated this issue by installing phpredis and enabling it in php.ini with extension=redis.so

This code causes the Call to undefined method Redis::connection() error:
$redis = Redis::connection();
$redis->set('name', 'Dave');
$name = $redis->get('name');

This code works:
Cache::forever('name', 'Dave');
$name = Cache::get('name');

Cache driver is set to 'redis'.

Changing the class alias to RedisL4 in app/config/app.php and then using this code solves the problem:

$redis = RedisL4::connection();
$redis->set('name', 'Dave');
$name = $redis->get('name');

Looks like @JoostK and @taylorotwell were spot on.

Yes, change your alias in app configuration to fix this.

Is this really fixed? I think it is no fix to have anybody change the alias by hand.

Found out that it is mentioned in the documentation already. Sorry to bother.

From the documentation:

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.

@cspiegl I would agree with you.

My issue is that I installed php-redis, so the class name of Redis is occupied. I change laravel Redis to LRedis in app.php. Solved.

'LRedis' => 'IlluminateSupportFacadesRedis',// php-redis occupied class name 'Redis'. I just change the key name.

I have got the same problem, then I find that I had installed the redis extenstion of php, then I remove it ,then it solved

Changing the alias is a better solution to removing the php extension. The php extension will improve performance.

@GrahamCampbell How does it improve the performance?

@dcarrith thanks a lot! change the alias is useful :)

@dcarrith ,

Thanks. Confirmed that changing alias fixed the problem.

I have php 5.5.10 installed on my MBP and didn't know that it came with phpredis.

For those who want to see if their PHP has redis client , do this in console:

php -m | grep redis

If you see something, then you have redis php installed. Follow @dcarrith 's method to change redis alias in app.php

The solution works when working in browser, but artisan seem to have some issues.

'RedisL4' => 'IlluminateSupportFacadesRedis',
Change alias in app configuration can be fix it. But why?

There is some conflict with the built-in laravel redis and php-redis

Why not change the alias to AppRedis or something?!

so strange,who can explain it

I meet the same question just now.

Here is another silly solution on Laravel!

First I tried renaming Redis and it worked. But it looked awkward, then I just added
"use IlluminateSupportFacadesRedis" on the classes I needed and it also worked. No need to rename the Alias.

For any weary travelers, I wanted to post a follow-up to this issue that is current as of Laravel 5.

To solve this issue, you have two options.

Renaming the Alias

Originally suggested by @dcarrith above. In the file config/app.php at _approximately_ line 194, you can rename the Redis facade like so:

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

At which point, you can access it by modifying your class as follows:

<?php namespace foo\bar;

use RedisL5;

Including the proper Illuminate\Redis driver

Originally suggested by @JoostK and @eduardocruz above. Simply include the proper driver and be done with it.

<?php namespace foo\bar;

use Illuminate\Support\Facades\Redis;

IMO, the second option is much cleaner.

@GrahamCampbell I am interested too to know why the php redis extension would improve performance as @Burnett01 already asked ?
Thanks

@trompx @Burnett01 php redis extension will improve performance because they are executed as C / C++ codes and not php code.
(Think of .so files)

I got a guide here in laracast how to set it up on php7
if you plan to use phpredis

https://laracasts.com/discuss/channels/tips/tutorial-guide-installing-php-redis-on-fresh-install-homestead-with-php7

php redis ext have the class name Redis
and laravel alias the redis facade named the same as Redis
so in the code use Redis is the php ext Redis class
the question has exist in the version 5.2

aliase the redis facade as other name
and use other name as Redis

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:wampbinphpphp-XXXXext
Laragon -> C:laragonbinphpphp-XXXext

Edit the php.ini file adding

extension=php_redis.dll

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

use IlluminateSupportFacadesRedis;

Was this page helpful?
0 / 5 - 0 ratings