资料参考:
Yaf是一个C语言编写的PHP框架,以php扩展的形式. 是 laruence(鸟哥) 的作品
laruence 是PHP 开发组成员, PECL 开发者. Yaf, Taint等Pecl扩展作者.
Yaf 相关文章 http://www.laruence.com/tag/yaf 在线手册
具体看 官方提供的例子
http://achun.iteye.com/blog/1473126
框架目录参考:
- .htaccess // Rewrite rules + public | - index.php // Application entry | + css | + js | + img + conf | - application.ini // Configure - application/ - Bootstrap.php // Bootstrap + controllers - Index.php // Default controller + views |+ index - index.phtml // View template for default controller - library - models // Models - plugins // Plugins
入口文件:
define ("APPLICATION_PATH", dirname(__FILE__) . "/application"); //var_dump(Yaf_Application::app()); $application = new Yaf_Application("conf/sample.ini"); //var_dump(Yaf_Application::app());exit; /* 如果打开flushIstantly, 则视图渲染结果会直接发送给请求端 * 而不会写入Response对象 */ //$application->getDispatcher()->flushInstantly(TRUE); /* 如果没有关闭自动response(通过Yaf_Dispatcher::getInstance()->returnResponse(TRUE)), * 则$response会被自动输出, 此处也不需要再次输出Response */ $response = $application //->bootstrap()/*bootstrap是可选的调用*/ ->run()/*执行*/;
application类实例化之后:
object(Yaf_Application)[1] protected 'config' => object(Yaf_Config_Ini)[2] protected '_config' => array (size=4) 'yaf' => array (size=7) ... 'smarty' => array (size=5) ... 'routes' => array (size=4) ... 'webroot' => string 'http://www.ap.com' (length=17) protected '_readonly' => boolean true protected 'dispatcher' => object(Yaf_Dispatcher)[4] protected '_router' => object(Yaf_Router)[5] protected '_routes' => array (size=1) ... protected '_current' => null protected '_view' => null protected '_request' => object(Yaf_Request_Http)[3] public 'module' => null public 'controller' => null public 'action' => null public 'method' => string 'GET' (length=3) protected 'params' => array (size=0) ... protected 'language' => null protected '_exception' => null protected '_base_uri' => string '' (length=0) protected 'uri' => string '/' (length=1) protected 'dispatched' => boolean false protected 'routed' => boolean false protected '_plugins' => array (size=0) empty protected '_auto_render' => boolean true protected '_return_response' => boolean false protected '_instantly_flush' => boolean false protected '_default_module' => string 'Index' (length=5) protected '_default_controller' => string 'Index' (length=5) protected '_default_action' => string 'index' (length=5) protected '_modules' => array (size=1) 0 => string 'Index' (length=5) protected '_running' => boolean false protected '_environ' => string 'product' (length=7) protected '_err_no' => int 0 protected '_err_msg' => string '' (length=0)
以上可以看出application里包含的属性主要有dispatch,config,modules,核心是dispatch,他包含了router,request,view,plugs等
以下说明下application的实例化过程:
1.判断单例是否存在,self:app()方法返回单例 2.通过实例化的第二个参数赋值$_environ 3.通过_loadConfig()方法传入实例化的第一个参数,得到config对象,如果$_environ为null,就从php.ini获取配置,默认是product,得到的的config对象会根据这个参数进行筛选配置结果 通过parseOptions方法把配置信息转为数组存入_options变量, 4.实例化Yaf_Request_Http对象new Yaf_Request_Http(); 得到_requestUri,得到_baseUri,得到method 5.获取单例Yaf_Dispatcher::getInstance(); 设置默认的action,control,module,这些来字配置 实例化Yaf_Router,默认添加$this->addRoute('_default', new Yaf_Route_Static()); 6.通过$this->_dispatcher->setRequest($request);把Yaf_Request_Http对象注入Yaf_Dispatcher 7.加载load单例,设置全局与项目lib路径,注册autoload方法 8.根据throwException配置,注册错误处理方法
执行bootstrap,这个是可选的启动过程,以下是bootstrap的过程:
1.根据配置获取bootstrap文件位置,默认在appDirectory下 2.执行Yaf_Loader::import($bootstrap)方法,参数为bootstrap文件地址 该方法就是一个include的包装器,相当于include的bootstrap文件 3.实例化bootstrap类,通过反射机制执行执行所有init开头的方法,参数为dispatch,这里一般执行初始化配置,添加插件,添加路由,添加模板机制
执行run:
1.检查app是否已经在运行 2.如果没有运行设置running = true,调用dispatch实例的dispatch方法return $this->_dispatcher->dispatch();
dispatch实例的dispatch方法:
1.设置request,通过request类型实例化response对象(http,cli) 2.判断请求有没有被路由,如果已经路由过且各路由相关参数为空,设置dispatch请求属性,如果没有设置路由过,执行过程如下 运行所有注册了routerStartup的插件,参数为request,response 执行路由实例的route()方法;参数为request对象 查找所有注册的路由,从后向前匹配,如果匹配成功设置request已被路 由过,此时request参数已经有module,control,action,params参数 各路由相关参数为空,设置dispatch请求属性 运行所有运行了routerShutdown的插件参数为request,response 3.实例化view对象,Yaf_View_Simple 4.运行所有注册了dispatchLoopStartup的插件,参数为request,response 5.从配置forward_limit获取最大分发次数,分发过程如下 如果被分发完成字段为false切没超过最大分发次数,执行如下 运行所有注册了preDispatch的插件,参数为request,response 执行$this->handle($request, $response, $view); 主要是查找control目录的类,执行action方法,或者查查action目录执行action方法,如果结果不是返回的false,可以根据_auto_render,_instantly_flush属性判断是否自动渲染或者自动展现 重设置设置dispatch请求属性 运行所有注册了postDispatch的插件,参数为request,response 如果分发完毕 运行所有注册了dispatchLoopShutdown的插件,参数为request,response 6.根据配置返回response对象
上面第5项说的渲染或者展现是通过control类的rander和display方法实现的,预测是加载视图并输出。
实际的代码片段,例如配置路由,插件,获取请求参数,返回数据,以及一些功能扩展,例如数据库连接,缓存等下篇测试。