Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up PhpRedis in Laravel 5+?

Background
Attempting to use PhpRedis in Laravel 5.3 on a Mac OSX local server
running Apache 2.4.18, Php 7.0.14 and homebrew
...without requiring additional (non-official) composer libraries

Redis is installed via homebrew install redis and working
tested by redis-cli ping which gives PONG

PhpRedis installed via homebrew install php70-redis and working
tested by php -r "if (new Redis() == true){ echo \"\r\n OK \r\n\"; }" which gives OK


Setup
With the documentation and this SO Laravel 4 solution I do the following:

  1. change (or comment & add) alias definition in app/config/app.php from
    'Redis' => 'Illuminate\Support\Facades\Redis'
    to
    'LRedis' => 'Illuminate\Support\Facades\Redis'
  2. add client definition to the redis database definition in config/database.php
    'client' => 'phpredis',
  3. run composer dump-autoload and php artisan optimize
  4. use that renamed alias in an example route code:

    Route::get('redistesturl', function () { $app = LRedis::connection(); $app->set("name", "Bob Cool"); print_r($app->get("name")); });


Errors

FatalThrowableError in Database.php line 62:
Class 'Predis\Client' not found

Also tested calling redis within route as per SO answer mentioned above :

$redis = Illuminate\Support\Facades\Redis::connection();

...but get same error

If I try to access LRedis class from within a controller like this:

use Illuminate\Support\Facades\Redis;

class MyController extends Controller
{
    public function redistest(){
        $redis = LRedis::connection();
        $redis->set('name', 'Bob Cool');
        return $redis->get('name');
    }
}

I get the following error:

FatalThrowableError in Preferences.php line 15:
Class 'App\Http\Controllers\LRedis' not found

Notes
Tested Predis and got it working fine by only adding the official predis library as specified in the docs.

I can get PhpRedis to work fine on my system (with the same route & controller examples) if I use an additional library like this one by following this Laracast ...however this question is specifically:

"How to set up PhpRedis in Laravel 5+ without additional composer libraries?"

like image 542
goredwards Avatar asked Dec 22 '16 20:12

goredwards


People also ask

Could not connect to redis at 127.0.0.1 6379 no connection could be made because the target machine actively refused it?

Firewall restriction is another common reason that can trigger the “could not connect to Redis connection refused”. By default Redis server listen to the TCP port 6379. If another application is using the port or if the firewall restrictions blocks the port, it can trigger the connection refused error.

How does redis integrate with laravel?

Redis in Laravel In order to use Redis with Laravel, firstly yo will have to install the predis/predis package. You can install it via composer. Just run this command and you're done. Apart from this, you can also install php redis, a php extension via PECL.

What is predis in laravel?

predis use pure php code to communicate with redis server.so there is no local php extension requirements. but it's a little slower. so you can deploy you application to any webhost that support php. As I remember. laravel default use predis to make it minial requirements.

Does laravel need redis?

So, Laravel has a built-in caching feature that is supported by Redis. Redis keeps its database completely in memory, using disk only for persistence. So that the response will still be slower when the data is not in Redis yet.


1 Answers

FatalThrowableError in Preferences.php line 15:
Class 'App\Http\Controllers\LRedis' not found

You have not include namespace for LRedis class

<?php
use LRedis
like image 190
Abhijeet Umbarkar Avatar answered Oct 13 '22 11:10

Abhijeet Umbarkar