Class 'Redis' not found in Lumen - php

Lumen Version: 6.0
PHP Version: 7.2
Database Driver & Version: MySql 5.7, Redis
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.

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

You can modify config/database.php.
because lumen6 redis default drive used phpredis.
add .env file like this.
REDIS_CLIENT=predis

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

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),
],
]

Related

Lumen Redis queue connection error. Error while reading line from the server. [tcp://xxxxxx:25061]

I am trying to use redis as QUEUE_CONNECTION in Lumen 8.0.
If I use the redis from the local server [127.0.0.1:6379] Then it's working fine.
But I want to use a external redis server from Digital ocean.
Here is my config in config/queue.php
'redis' => [
'driver' => 'redis',
'connection' => env('QUEUE_REDIS_CONNECTION', 'default'),
'queue' => 'default',
'retry_after' => 90,
'block_for' => null,
'read_write_timeout' => -1,
]
But I am getting this error :
I have installed these packages :
"predis/predis": "^1.1",
"aws/aws-sdk-php": "~3.0",
"illuminate/redis": "^8.25"
Also configured in the .env file like this:
REDIS_CLIENT=predis
REDIS_HOST="this.is.my.redis.server.ondigitalocean.com"
REDIS_PASSWORD=xxxxxxxx
REDIS_PORT=25061
REDIS_SCHEME=tls
Am I missing anything else ?
make sure the port is allowed on the droplet's firewall.

Laravel Sqlite database cannot be connected to

When I navigate to my app on my server (Ubuntu) i get the following:
Data: { message: 'could not find driver (SQL: select * from "projects" order by "created_at" desc)',
exception: 'Illuminate\\Database\\QueryException',
file: '/root/project/api/vendor/laravel/framework/src/Illuminate/Database/Connection.php',
line: 664,
trace:
[ { file: '/root/project/api/vendor/laravel/framework/src/Illuminate/Database/Connection.php',
line: 624,
function: 'runQueryCallback',
class: 'Illuminate\\Database\\Connection',
type: '->' },
However locally it works. So i thought perhaps the driver is not install, but when I run the following it shows the I have sqlite installed:
sqlite3 -version
3.19.3 2017-06-08 14:26:16 0ee482a1e0eae22e08edc8978c9733a96603d4509645f348ebf55b579e89636b
My database config looks like this with the default being sqlite:
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.db')),
'prefix' => '',
],
Whats my next step to figure out why this wont work?
You have sqlite3 installed, but not php's driver for it. The driver is also called sqlite3, so for e.g. Ubuntu/Debian that would be sudo apt-get install php7.2-sqlite3.

Laravel Scout production error with TNTSearch driver

Searching using the TNTSearch driver works in a Homestead environment however on production it returns error: the below error,
Symfony\Component\Debug\Exception\FatalThrowableError: Class
'AlgoliaSearch\Version' not found on
vendor/laravel/scout/src/EngineManager.php:31
However my .env has SCOUT_DRIVER=tntsearch and the config file scout.php has:
'driver' => env('SCOUT_DRIVER', 'tntsearch'),
'tntsearch' => [
'storage' => storage_path(),
'fuzziness' => env('TNTSEARCH_FUZZINESS', false),
'fuzzy' => [
'prefix_length' => 2,
'max_expansions' => 50,
'distance' => 2
],
'asYouType' => false,
'searchBoolean' => env('TNTSEARCH_BOOLEAN', false),
]
The problem is that I am not using Algolia search and my composer file has Scout and TNTSearch driver. The search works in my local Homestead environment just not on the production server.
Confirm that SCOUT_DRIVER=tntsearch has been added to your .env file.
For me personally, I had added SCOUT_DRIVER=tntsearch to my local .env file, but not my .env file for the environment with the issue. Don't forget to run php artisan config:clear after adding the env var.
Thanks to #m33bo for pointing me in the right direction!
I worked it out, I had uploaded my project but for some reason the .index file that is needed sync'd but did not work. If this happens to you on live make sure you Git or SVN or whatever the index or run php artisan scout:import App\\Your\\Model

spatie/laravel-backup "mysqldump" doesn't recognized when I run it through Artisan class

I'm using spatie/laravel-backup in a WAMP localhost.
It works fine when I type manually in the windows cmd:
php artisan backup:run
But when I try to run the backup using the laravel Artisan class:
Artisan::call('backup:run');
It throw an error:
'mysqldump' not recognized ...
In the laravel mysql config I've also specified the path to the dumper:
'mysql' => [
'driver' => 'mysql',
// ...
'dump' => [
'dump_binary_path' => 'E:/wamp/wamp64/bin/mysql/mysql5.7.9/bin',
],
],
How can i fix that?
EDIT
Probably it's just support "bug" for windows (finded out thanks Loek's answer), as the author says, so can I run a backup in a controller without a command safely? maybe with something like:
use Spatie\Backup\Tasks\Backup\BackupJobFactory;
BackupJobFactory::createFromArray(config('laravel-backup'))->run();
As the command itself.
I believe it's the forward slashes. Try this:
'mysql' => [
'driver' => 'mysql',
// ...
'dump' => [
'dump_binary_path' => 'E:\\wamp\\wamp64\\bin\\mysql\\mysql5.7.9\\bin',
],
],
EDIT
Support for Windows is wonky at best, with multiple "This package doesn't support Windows" comments from the creators on GitHub issues. This one is the latest: https://github.com/spatie/laravel-backup/issues/311
It could also be a permission problem. Executing from the command line is probably happening from another user than executing from the web server, so Windows is denying access to mysqldump.
2nd edit
As long as you make sure the controller only gets called when it needs to be, I don't see why this wouldn't work!

Unsupported driver [mongodb] Laravel

I am using Laravel 5.1 with MongoDB. I already installed
https://github.com/jenssegers/laravel-mongodb
Problem is I am getting this error when running my queries:
Unsupported driver [mongodb]
But I already have my drivers installed in my machine. I am using Windows 10 and WAMP. I can confirm it with php info:
I
also check all my php.ini files which i already included my DLL's and looks fine. I have my drivers also.
My jessengers version is v3.0.2
I think you are missing the line :
Add the service provider in config/app.php:
Jenssegers\Mongodb\MongodbServiceProvider::class,
Refer the Documentation.
https://github.com/jenssegers/laravel-mongodb
Run this :
composer require jenssegers/mongodb:*
There are 2 mongodb drivers in php:
The legacy one: http://php.net/manual/en/book.mongo.php
The recommended one: http://php.net/manual/en/set.mongodb.php
laravel-mongodb Readme reads:
WARNING: The old mongo PHP driver is not supported anymore in versions >= 3.0
Laravel 5.1 can be used with either jenssegers/mongodb v2.2 or jenssegers/mongodb v3.0. The former requires legacy driver, the later requires new driver.
Check which version of jenssegers/mongodb you are using, and make it agree with php mongodb driver.
composer require jenssegers/mongodb:*
install jenssegers package by above command
then
composer update
you need to make database related changes in .env file :
DB_CONNECTION=mongodb
DB_HOST=localhost
DB_PORT=27017
DB_DATABASE=mongodb
DB_USERNAME=
DB_PASSWORD=
and in config/database.php file :
'default' => env('DB_CONNECTION', 'mongodb')
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE','mongodb'),
'username' => env(''),
'password' => env(''),
],
modify above parameter as per your appropriate credentials.
Also in config/app.php
Jenssegers\Mongodb\MongodbServiceProvider::class,
if you are using lumen, make sure you are adding the service provider before $app->withEloquent();
Like this
$app->register(Jenssegers\Mongodb\MongodbServiceProvider::class);
$app->withEloquent();

Categories