当前位置: 移动技术网 > IT编程>开发语言>PHP > Zend的MVC机制使用分析(二)

Zend的MVC机制使用分析(二)

2019年06月05日  | 移动技术网IT编程  | 我要评论

2012电影下载地址,3u8814,点金胜手3

接着上面的一篇


把代码贴上来

复制代码 代码如下:

$front = zend_controller_front::getinstance();
zend_layout::startmvc(array('layoutpath' => usvn_layouts_dir));

$front->setrequest(new zend_controller_request_http());
$front->throwexceptions(true);
$front->setbaseurl($config->url->base);

$router = new zend_controller_router_rewrite();
$routes_config = new usvn_config_ini(usvn_routes_config_file, usvn_config_section);
$router->addconfig($routes_config, 'routes');
$front->setrouter($router);
$front->setcontrollerdirectory(usvn_controllers_dir);

$front->dispatch();

上一篇把前两句getinstance和startmvc两个函数已经读完了,下面是继续分析后面的代码

setrequest($request) 这里是判断request是否是继承自zend_controller_request_abstract,如果是的话就把front的_request赋值为它。

这里需要了解下什么是zend_controller_request_abstract,它是所有request抽象出来的抽象类。zend已经提供了两个实现类,zend_controller_request_http和zend_controller_request_simple,一般我们搭建服务器都是http请求,所以你的项目如果需要重新继承的话,一般都直接继承zend_controller_request_http。

zend_controller_request_http中我们经常会使用到的getquery,getcookie,getrequesturi,getbasepath,getparams,getheader等这些http通常的选项都已经有了。

继续讲它的基类zend_controller_request_abstract,这个类的方法包含:

回到代码
 

$front->setrequest(new zend_controller_request_http());这里调用了zend_controller_request_http的构造函数,构造函数在第一次调用的时候是$this->setrequesturi();其中的setrequesturi很多都是直接使用$_server这个php全局变量中的数据来获取requesturi的。

setrequesturi可以学到的是在不同的服务器中如何获取requesturi(特别是在iis中的$server中不同的变量组合有不同的含义),比如http://172.23.11.160/usvn/item/usvn_test 这个url,它的requesturi就是/usvn/item/usvn_test

 

$front->throwexceptions(true); 将内部的_throwexceptions标志位设置为true;

$front->setbaseurl("/usvn")这个做了两件事情,首先是设置front内部的_baseurl属性,其次调用request的setbaseurl,也是设置zend_controller_request_http的内部_baseurl属性。


$router = new zend_controller_router_rewrite();

$routes_config = new usvn_config_ini(usvn_routes_config_file, usvn_config_section);

$router->addconfig($routes_config, 'routes');

$front->setrouter($router);

下面这三行就直接说,实际上就是使用zend的router模块使用配置文件,router使用setrouter放入front里面。


最后一句


$front->dispatch();

这个函数也是最核心的一个函数。

这个函数首先注册了一个插件zend_controller_plugin_errorhandler,index为100,把插件的顺序放在最后。

 

第二步存放了一个helper,zend_controller_action_helper_viewrenderer,index为-80

下面实例化了request,request是一个zend_controller_request_http类型。并将request的baseurl设置为前面设置过的_baseurl,就是"/usvn/item/usvn_test"

接着实例化了response,response是一个zend_controller_response_http();

下面使用plugins来对request和response进行设置,首先实际调用了zend_controller_plugin_broker的setrequest函数,这个函数循环遍历broker管理的所有插件,调用插件的setrequest($request)函数(如果有的话)。

 

接下来初始化router,和设置router的参数。router已经在前面设置过了,就是zend_controller_router_rewrite类型

初始化分发器dispatcher,分发器我们是第一次看到,zend_controller_dispatcher_standard类。分发器以后再说。


下面的流程:

调用插件的routestartup对request进行处理

调用router的route处理request

调用插件的routeshutdown对request进行处理

调用插件的dispatchloopstartup对request进行处理

进入循环分发过程

调用插件的predispatch对request进行处理

调用dispatcher的dispatch处理request和response

调用插件的postdispatch对request进行处理

跳出循环分发过程

调用插件的dispatchloopshutdown对request进行处理

发送response

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网