laravelredislaravel-5.5predisphpredis

Laravel 5.5 Redis problem - Call to undefined method Illuminate\Support\Facades\Redis::connect()


I moved my Laravel 5.5 application to another server - I use exactly the same code there (did a git clone) with exactly the same composer.json and composer.lock files (even the NGINX configuration is the same).

When I run my application I get the following error:

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to undefined method Illuminate\Support\Facades\Redis::connect()

Here is the code:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
...
public function somefunction() {
    $redis = new \Redis();
    $redis->connect(env('REDIS_HOST')); <-------------
...

The composer package predis/predis is installed and I have no php-redis on my system.

On both systems (debian) redis is installed and runs on 127.0.0.1. Both systems use the same configuration in .env and in config/*:

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

The only thing, which is different is, that on one system (old one) I'm runnning php7.0 and on the new system I run php7.3 - I switched to php7.0 on the new system to check if that's the error, but I still get the exception.

Once again - on my other server everything is running fine with exactly the same code, which frustrates me - I can't figure out why this is happening.


Solution

  • I think these are some basic steps you need to check :

    1) Firstly, make sure you have phpredis PHP extension installed

    2) If you have cluster in your redis configuration, then make sure to set it to false : see

    'cluster' => false,

    3) Try to check that 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.

    4) If you're running in a *nix environment, you can check the netstat output to see if Redis is listening on whatever port (say 6379) 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

    5) in app/config/cache.php, set the driver to redis:

    'driver' => 'redis'

    6) Try using redis as the driver in app/config/session.php:

    'driver' => 'redis'

    7) add the following at the top of your source:

    use Illuminate\Redis\Database as Redis;

    or

    "use Illuminate\Support\Facades\Redis"

    8) Try Changing the class alias to RedisL4 in app/config/app.php like

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

    and then using this code probably solves the problem:

    $redis = RedisL4::connect(); or more importantly its "connection" and not "connect" so

    $redis = RedisL4::connection(Your-Connection-Here);