jeudi 22 décembre 2016

using 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 plugins / 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
  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

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 additional predis library as specified in the docs.

I can get PhpRedis (with the same route) to work fine on my system 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 libraries?"



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire