忽然之间的博客

Thoughts, stories and ideas.

Lumen中使用phpredis


# 安装Lumen composer config -g repositories.packagist composer http://packagist.phpcomposer.com composer create-project --prefer-dist laravel/lumen lumen # redis 包 composer require "predis/predis ~1.0" composer require "illuminate/redis 5.3.*"

vim bootstrap/app.php //Lumen中使用PhpRedis# //绑定后即可通过 app('phpredis') 直接使用 PhpRedis 了,具体使用方法可以看相应的官方文档。 $app->singleton('redis', function() { try { $redis = new \Redis(); $redis->pconnect('127.0.0.1'); //建立连接 return $redis; } catch (Exception $e) { echo $e->getMessage(); } }); unset($app->availableBindings['redis']); # controller中使用 vim ExampleController.php <?php namespace App\Http\Controllers; class ExampleController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { // } public function index() { $redis = app('redis'); var_dump($redis); $redis->set('kkk', 'vvv'); } }