5

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.

manifestor
  • 1,352
  • 6
  • 19
  • 34
  • Do you have "cluster" (if it exists there) set to "false" in the Redis configuration? – Jitendra Ahuja Apr 04 '19 at 11:04
  • Try "connection" instead of "connect". See `$redis->connection(env('REDIS_HOST')); ` – Jitendra Ahuja Apr 04 '19 at 12:00
  • Yes, `connection` works. I fugured that out already, but why? Also I don't want to change the code. – manifestor Apr 04 '19 at 12:06
  • Because You get a Redis instance by calling the `Redis::connection();` method. This will give you an instance of the default Redis server. You may also pass the connection or cluster name to the connection method to get a specific server or cluster as defined in your Redis configuration – Jitendra Ahuja Apr 04 '19 at 12:10
  • Why is it working on another system then with the same code and `connect()`? It does not work, becauase if you change it to `connection()` you will get strange syntax errors in the `predis/predis` core files, which is weird. – manifestor Apr 04 '19 at 12:18
  • phpredis is a native extension, it's bound to be more efficient than predis even if the development is less active. So its better to use in my recommendation. Also lets try pconnect() as it might be possible that once your connection persisted and now it isn't. It checks and connect if your connection was once established – Jitendra Ahuja Apr 04 '19 at 12:19
  • You are using Predis and then you write `$redis = new \Redis();`, then ofcourse it will go for redis' methods to work and not predis'. Try to add `require('./Predis.php'); $redis = new Predis\Client('redis://$z'); $redis->connect();` predis has `connect()` while for redis its `connection()` – Jitendra Ahuja Apr 04 '19 at 12:23
  • 1
    @JitendraAhuja - you are absolutely right! I messed up stuff in my hurry. I was thinking I was using the predis extension all the time. Man, thank you so much for your efforts and time! I made a big mistake, was and still is a very stressful day. Once again, thank you! – manifestor Apr 04 '19 at 12:35
  • Oh thats absolutely fine ! Happy to help you my friend :} – Jitendra Ahuja Apr 04 '19 at 12:38

2 Answers2

2

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

Jitendra Ahuja
  • 749
  • 3
  • 9
  • 22
1

Ensure you are passing the REDIS_CLIENT properly.

For me, I had installed predis via composer, but REDIS_CLIENT was set to phpredis.

I had to change REDIS_CLIENT to predis in my database.php / .env file.

REDIS_CLIENT=predis

user3785966
  • 2,578
  • 26
  • 18