微度云课堂


yaf视频教程的插件的讲解,插件plugin支持7种hook,大家可以去官方查看官方文档,这部分主要讲yaf的插件的使用和一些类的简单分析,具体的大家可以看视频!!

Demo.php 插件实例代码

class DemoPlugin extends Yaf_Plugin_Abstract{
    public function routerStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response){
        if(strtolower($request->getMethod())=="get"){
            if($request->get("id")==1){
                echo "插件启动<br>";
            }
        }
    }
    public function routerShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response){
        if(strtolower($request->getMethod())=="get"){
            if($request->get("id")==2){
                echo "插件关闭";
            }
        }
    }

    public function dispatchLoopStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response){
        echo "Plugin dispatchLoopStartup called <br/>\n";
    }

    public function postDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response){
        echo "Plugin postDispatch called <br/>\n";
    }

    public function preDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response){
        echo "Plugin preDispatch called <br/>\n";
        var_dump($request);
    }

    public function preResponse(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response){
        echo "Plugin preResponse called <br/>\n";
    }

    public function dispatchLoopShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response){
        echo "Plugin dispatchLoopShutdown called <br/>\n";
    }
}

bootstrap.php配置代码

class Bootstrap extends Yaf_Bootstrap_Abstract{
    public function _initConfig(Yaf_Dispatcher $dispatcher){
            //var_dump($dispatcher->getRequest());
            $config = Yaf_Application::app()->getConfig();
            Yaf_Registry::set("config", $config);
    }

    public function _initRouter(){

    }

    public function _initSpaceName(){
        $loader = Yaf_Loader::getInstance()->registerLocalNamespace(array("Test","Dum"));
        Yaf_Registry::set("loader", $loader);
    }


    public function _initPlugin(Yaf_Dispatcher $dispatcher){
        $demo = new DemoPlugin();
        $dispatcher->registerPlugin($demo);
    }
}

点赞(2) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部