yaf视频教程-插件的使用和讲解

widuu 2013-12-24 PHP技术 5,122 7 喜欢 (2)

微度云课堂

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);
    }
}

转载请注明来自微度网络,本文标题:《yaf视频教程-插件的使用和讲解》

喜欢 2 发布评论
Top