1.Yaf之配置文件application.ini
conf/application.ini
1.关于yaf中的配置信息大部分都写在这里了!我们先来看一个例子
[common]
application.directory=APP_PATH"/application/"
[product : common]
2.打印出配置信息看一下:
application/controllers/Index.php
<?php
class IndexController extends Yaf_Controller_Abstract {
public function indexAction() {
//读取配置文件
$config = Yaf_Application::app()->getConfig();
//打印配置信息
echo '<pre>';
print_r($config);
echo '</pre>';
}
}
?>
3.查看配置信息
4.那么怎么加入多组配置信息呢,比如我有两个redis服务器
很简单,可以看出上面的配置文件,里面有一个common组,下面列出的配置信息打印在了浏览器上!那么我们就可以再添加一个redis的组
[common]
application.directory=APP_PATH"/application/"
[redis]
;用作缓存的redis服务器
redis.cache.host = 192.168.254.128
redis.cache.port = 6379
redis.cache.dbIndex = 1
;用作存储用户信息的redis服务器
redis.user.host = 192.168.254.128
redis.user.port = 6379
redis.user.dbIndex = 12
;别忘了在这里加上你要读取的配置组名
[product : common : redis]
2.Yaf之Bootstrap
这是yaf提供的一个自带的钩子功能,这里提供了6个Hook分别是:
Boostrap.php中以_init开头的方法会依次执行。
具体使用方法:
application/Bootstrap.php
class Bootstrap extends Yaf_Bootstrap_Abstract {
private $_config;
/*
* 注册一个插件
* 插件的目录是在application_directory/plugins
*/
public function _initPlugin(Yaf_Dispatcher $dispatcher) {
//加载plugin下面的User.php插件
$user = new UserPlugin();
$dispatcher->registerPlugin($user);
}
//可以添加其他以_init开头的方法
public function _initConfig(Yaf_Dispatcher $dispatcher) {
$this->_config = Yaf_Application::app()->getConfig();
Yaf_Registry::set("config", $this->_config);
}
}
插件application/plugins/User.php
class UserPlugin extends Yaf_Plugin_Abstact {
public function routerStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
echo "在路由出发之前";
}
//下面就可以放入上面钩子中的方法
}
入口文件index.php
<?php
define("APP_PATH", realpath(dirname(__FILE__)));
$app = new Yaf_Application(APP_PATH."/conf/application.ini");
$app->bootstrap()->run();
?>