- Lumen Version: 6.0
- PHP Version: 7.2
- Database Driver & Version: MySql 5.7, Redis
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
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 can modify config/database.php.
because lumen6 redis default drive used phpredis.
add .env file like this.
My steps to fix that in Lumen 7:
composer require illuminate/redis:"^7.0"
yum --enablerepo=epel -y install php-pecl-redis
composer require predis/predis:"^1.0"
'client' => 'predis'
. So the config is:If you are using
Laravel 8
, in thedatabase.php
file, replace the following line:to:
then, add the
predis
dependency withcomposer
: