yaf3 mysql_学习yaf(三)插件

class Bootstrap extends Yaf_Bootstrap_Abstract{

public function _initPlugin(Yaf_Dispatcher $dispatcher) {

$user = new UserPlugin();

$dispatcher->registerPlugin($user);

}

}

插件目录

一般的, 插件应该放置在APPLICATION_PATH下的plugins目录, 这样在自动加载的时候, 加载器通过类名, 发现这是个插件类, 就会在这个目录下查找.

当然, 插件也可以放在任何你想防止的地方, 只要你能把这个类加载进来就可以

一个UserPlugin插件的使用

首先在Plugins目录下 新建User.php文件,因为yaf配置默认识别为UserPlugin.php,你可以在配置中修改是否携带后缀

/**

* 插件类定义

* User.php

*/

class UserPlugin extends Yaf_Plugin_Abstract {

//在路由之前触发,这个是7个事件中, 最早的一个. 但是一些全局自定的工作, 还是应该放在Bootstrap中去完成

public function routerStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {

echo "Plugin routerStartup called
\n";

}

//路由结束之后触发,此时路由一定正确完成, 否则这个事件不会触发

public function routerShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {

echo "Plugin routerShutdown called
\n";

}

//分发循环开始之前被触发

public function dispatchLoopStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {

echo "Plugin DispatchLoopStartup called
\n";

}

//分发之前触发 如果在一个请求处理过程中, 发生了forward, 则这个事件会被触发多次

public function preDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {

echo "Plugin PreDispatch called
\n";

}

//分发结束之后触发,此时动作已经执行结束, 视图也已经渲染完成. 和preDispatch类似, 此事件也可能触发多次

public function postDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {

echo "Plugin postDispatch called
\n";

}

//分发循环结束之后触发,此时表示所有的业务逻辑都已经运行完成, 但是响应还没有发送

public function dispatchLoopShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {

echo "Plugin DispatchLoopShutdown called
\n";

}

public function preResponse(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {

echo "Plugin PreResponse called
\n";

}

}

在Bootstrap中注册插件

/**

* 插件的注册

*/

public function _initPlugin(Yaf_Dispatcher $dispatcher) {

$user = new UserPlugin();

$dispatcher->registerPlugin($user);

}

这时候访问浏览器 输出如下

Plugin routerStartup called

Plugin routerShutdown called

Plugin DispatchLoopStartup called

Plugin PreDispatch called

Plugin postDispatch called

Plugin DispatchLoopShutdown called

Hello World

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值