使用composer构建PHP框架怎么把Redis引入

选择 nrk/predis 作为 Redis 驱动,修改composer.json增加一个 require 项:

"predis/predis": "*"

运行composer update完成安装,新建config/redis.php

<php
return array(
'host' => '127.0.0.1',
'port' => 6379
);

新建 assists/factory.php:

<?php
use Predis\Client;
class Factory{
public static function redis(){
return new Client(require BASE_PATH.'/config/redis.php');
}
}

然后运行 composer dump-autoload, 修改controllers/HomeController.php为:

<?php
class HomeController extends BaseController{
public function home(){
$redis = Factory::redis();
$redis->set('name','xiaoming');
echo $redis->get('name');
}
}

访问 nuf.dev/index.php/home,输出

xiaoming

说明成功引入predis

posted @ 2016-12-24 17:11  鼎峰Bruce  阅读(5381)  评论(0编辑  收藏  举报