skip to Main Content
  • Lumen Version: 6.0
  • PHP Version: 7.2
  • Database Driver & Version: MySql 5.7, Redis

image

Code

use IlluminateSupportFacadesRedis;

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

in app.php

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

$app->register(IlluminateRedisRedisServiceProvider::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.

4

Answers


  1. 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(IlluminateRedisRedisServiceProvider::class);
    
    #Add the following right below
    if (!class_exists('Redis')) {
        class_alias('IlluminateSupportFacadesRedis', 'Redis');
    }
    
    Login or Signup to reply.
  2. You can modify config/database.php.

    because lumen6 redis default drive used phpredis.

    add .env file like this.

    REDIS_CLIENT=predis
    
    Login or Signup to reply.
  3. 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),
              ],
          ]
      
    Login or Signup to reply.
  4. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search