Sharath
Sharath

Reputation: 2428

Class 'Redis' not found in Lumen

image

Code

use Illuminate\Support\Facades\Redis;

Redis::set($key, $data, 'EX', $expiry);

in app.php

$app->withFacades();
$app->withEloquent();

$app->register(Illuminate\Redis\RedisServiceProvider::class);

$app->configure('database');

Using the above code gives Class 'Redis' not found error. This error occurs only when the below packages are installed.

"illuminate/redis": "^6.5",
"illuminate/mail": "^6.5",
"laravel/lumen-framework": "^6.0",

With below packages which has lower versions it works without any error/issues.

"laravel/lumen-framework": "^5.8.*",
"illuminate/redis": "^5.8",
"illuminate/mail": "^5.8",

So why is it giving error when packages are upgraded.

Upvotes: 6

Views: 17200

Answers (4)

pableiros
pableiros

Reputation: 16022

If you are using Laravel 8, in the database.php file, replace the following line:

'client' => env('REDIS_CLIENT', 'phpredis')

to:

'client' => env('REDIS_CLIENT', 'predis')

then, add the predis dependency with composer:

composer require predis/predis

Upvotes: 14

Valeri
Valeri

Reputation: 327

My steps to fix that in Lumen 7:

  • install "illuminate/redis" package: composer require illuminate/redis:"^7.0"
  • install "php-redis" on my CentOS7: yum --enablerepo=epel -y install php-pecl-redis
  • install "predis" package: composer require predis/predis:"^1.0"
  • change the redis client to "predis" (by default its "phpredis"): 'client' => 'predis'. So the config is:
    'redis' => [
            'cluster' => false,
            'client' => 'predis',
            'default' => [
                'host'     => env('REDIS_HOST', '127.0.0.1'),
                'port'     => env('REDIS_PORT', 6379),
                'database' => env('REDIS_DATABASE', 0),
            ],
        ]
    

Upvotes: 1

bigface
bigface

Reputation: 146

You can modify config/database.php.

because lumen6 redis default drive used phpredis.

add .env file like this.

REDIS_CLIENT=predis

Upvotes: 12

DesmondCampbell
DesmondCampbell

Reputation: 31

Make sure you set up the PHP Redis extension and enable it.

Even after you do that, you will need to register an alias for Redis in your app.php file. It's clear that you referenced it with your use statement, but that is only visible in the class you are "using" it. The PHP Redis connector will need to reference it from somewhere globally, which is in the app.php file. Laravel comes with this already set-up, but unfortunately Lumen doesn't.

To be safe, wrap it with a check on class existence.

This is how I fixed the problem.

#You already have this:
$app->register(Illuminate\Redis\RedisServiceProvider::class);

#Add the following right below
if (!class_exists('Redis')) {
    class_alias('Illuminate\Support\Facades\Redis', 'Redis');
}

Upvotes: 3

Related Questions