search
HomeBackend DevelopmentPHP TutorialAnalysis of Zend's MVC mechanism usage (1)_PHP tutorial
Analysis of Zend's MVC mechanism usage (1)_PHP tutorialJul 21, 2016 pm 03:11 PM
controllergmvczendonecodeuseanalyzecopymechanismof

代码

复制代码 代码如下:

$front = Zend_Controller_Front::getInstance();
Zend_Layout::startMvc(array('layoutPath' => USVN_LAYOUTS_DIR));

$front->setRequest(new USVN_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);

Zend_Controller_Front::getInstance()->dispatch();

分析

首先看下Zend_Controller_Front::getInstance是调用单例模式,实例化了它的内部属性_plugins,实例化了一个Zend_Controller_Plugin_Broker类。

这个类是管理front的插件的类。先看一个Front中的方法public function registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null)

意思是如果你有一个自己的插件要插入使用的话,调用这个函数能把你自己的插件委托给Zend_Controller_Plugin_Broker使用。

如果你有愿望继续跟下去你会看到注册插件做的一件最根本的事情就是把request和response放入到你的插件中去(setRequest和setResponse)。

class Zend_Controller_Plugin_Broker extends Zend_Controller_Plugin_Abstract

这个实现了抽象类Zend_Controller_Plugin_Abstract。
Zend_Controller_Plugin_Abstract是所有插件的抽象类,所有用户自己定义的插件或者Zend已有的插件都要从这个类继承。这里就看到了,前端控制器Front就是使用broker作为用户插件注册。

这个抽象类可以被实现的函数有:

routeStartup: 在路由发送请求前被调用

routeShutdown:在路由完成请求后被调用

dispatchLoopStartup:在进入分发循环(dispatch loop)前被调用

Predispatch:在动作由分发器分发前被调用

postdispatch:在动作由路由器分发后被调用

dispatchLoopShutdown:在进入分发循环(dispatch loop)后被调用

 

我们还看到了getRequest, getResponse两个方法,我们可以通过他们分别从控制器中获取request对象和response对象

 

好了,扯远了,回到最开始的代码,Zend_Controller_Front::getInstance实际上来看做的事情就是注册了一个broker插件放到$front中。

 

下面一行代码

Zend_Layout::startMvc(array('layoutPath' => USVN_LAYOUTS_DIR));

看到Zend/Layout.php中,startMvc做了两件事:首先是调用自己的构造函数来实例化自己(切记带着initMvc参数为true),然后是设置参数。

Zend_Layout的构造函数比较复杂,就跟到里面看看。首先也是设置传递进来的参数$options,我们这个例子中是传递进来Array ( [layoutPath] => /var/www/html/usvn/app/layouts )这个array作为options,构造函数就是调用$this->setOptions($options);

这个setOptions做的事是根据array的每个key,调用$this->set$key($val);也就是说,以上面的例子来说,setOptions调用了setLayoutPath("/var/www/html/usvn/app/layouts")

顺藤摸瓜,setLayoutPath的功能是设置自己类的this->_layout为"/var/www/html/usvn/app/layouts", 然后设置_enable为true;这两个属性记住,以后会有使用的。

 

回退到Zend_Layout的构造函数,初始化options之后是调用了_initVarContainer();

这个函数做了这么个事情:

$this->_container = Zend_View_Helper_Placeholder_Registry::getRegistry()->getContainer(__CLASS__);

又出现了Zend_View_Helper_Placeholder_Registry(我翻译为:Zend视图助手注册表)

 

getRegistry() 将Zend_View_Helper_Placeholder_Registry作为key,Zend_View_Helper_Placeholder_Registry类的实例作为value注册到之前见过的Zend_Registry中。这个类的构造函数就什么事都没有。

 

getRegistry()返回了Zend_View_Helper_Placeholder_Registry实例,下面调用getContainer(__CLASS__)。 这里的__CLASS__是什么,当前调用的类,自然就是Zend_Layout了。这里是getContainer("Zend_Layout")

 

进入到getContainer里面,它调用了createContainer("Zend_Layout")。createContainer("Zend_Layout")是在Registry中以Zend_Layout为key,Zend_View_Helper_Placeholder_Container类为value的array。

 

Zend_View_Helper_Placeholder_Container实现抽象类Zend_View_Helper_Placeholder_Container_Abstract,这个抽象类实际上也是一个ArrayObject,这个在之前的文章有提到过了,是一个和泛型类一样的东东。

 

好了,这里不跟下去了,回头到Zend_Layout的构造函数

_initVarContainer结束了,下面是调用两个重要的函数:

$this->_setMvcEnabled(true);

$this->_initMvc();

Mvc大家一定很熟悉,我们来看看这里是怎么个MVC的

setMvcEnabled没什么特别,设置标志位this->_mvcEnabled

_initMvc做了两件事,_initPlugin和_initHelper。

先看initPlugin:

获取PluginClass,这里的pluginClass就是Zend_Layout_Controller_Plugin_Layout,可以看到,这里是作为一个插件的形式放进来的。

Then the instance of Zend_Controller_Front was obtained and called:

$front->registerPlugin(

new $pluginClass($this),

99

);

Remember the previous analysis of Zend_Controller_Front? There is a function of registerPlugin, which delegates the plug-in to the front broker for use. Some people will ask what the 99 at the end means? It is the index order of plug-ins. The later the plug-in is, the later the plug-in action will be executed.

Look at _initHelper below:

Get the helperClass, the helperClass here is Zend_Layout_Controller_Action_Helper_Layout

if (!Zend_Controller_Action_HelperBroker::hasHelper('layout')) {

. . .

Zend_Controller_Action_HelperBroker::getStack()->offsetSet(-90, new $helperClass($this));

}

If Action_HelperBroker does not have a layout helper

Execute the offsetSet command below. Pass in -90 and the Zend_Layout_Controller_Action_Helper_Layout instance as parameters.

The same relationship as the plugin, save the Zend_Layout_Controller_Action_Helper_Layout instance as value to this->_helpersByPriority and this->_helpersByNameRef

The -90 in front is the weight, and it is also necessary to ensure that this helper is called last (see the last line for krsort sorting)

Okay, this concludes the analysis of Layout's constructor.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326858.htmlTechArticleCode copy code The code is as follows: $front = Zend_Controller_Front::getInstance(); Zend_Layout::startMvc(array(' layoutPath' = USVN_LAYOUTS_DIR)); $front-setRequest(new USVN_Controlle...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何在技嘉主板上设置键盘启动功能 (技嘉主板启用键盘开机方式)如何在技嘉主板上设置键盘启动功能 (技嘉主板启用键盘开机方式)Dec 31, 2023 pm 05:15 PM

技嘉的主板怎么设置键盘开机首先,要支持键盘开机,一定是PS2键盘!!设置步骤如下:第一步:开机按Del或者F2进入bios,到bios的Advanced(高级)模式普通主板默认进入主板的EZ(简易)模式,需要按F7切换到高级模式,ROG系列主板默认进入bios的高级模式(我们用简体中文来示范)第二步:选择到——【高级】——【高级电源管理(APM)】第三步:找到选项【由PS2键盘唤醒】第四步:这个选项默认是Disabled(关闭)的,下拉之后可以看到三种不同的设置选择,分别是按【空格键】开机、按组

CS玩家的首选:推荐的电脑配置CS玩家的首选:推荐的电脑配置Jan 02, 2024 pm 04:26 PM

1.处理器在选择电脑配置时,处理器是至关重要的组件之一。对于玩CS这样的游戏来说,处理器的性能直接影响游戏的流畅度和反应速度。推荐选择IntelCorei5或i7系列的处理器,因为它们具有强大的多核处理能力和高频率,可以轻松应对CS的高要求。2.显卡显卡是游戏性能的重要因素之一。对于射击游戏如CS而言,显卡的性能直接影响游戏画面的清晰度和流畅度。建议选择NVIDIAGeForceGTX系列或AMDRadeonRX系列的显卡,它们具备出色的图形处理能力和高帧率输出,能够提供更好的游戏体验3.内存电

广联达软件电脑配置推荐;广联达软件对电脑的配置要求广联达软件电脑配置推荐;广联达软件对电脑的配置要求Jan 01, 2024 pm 12:52 PM

广联达软件是一家专注于建筑信息化领域的软件公司,其产品被广泛应用于建筑设计、施工、运营等各个环节。由于广联达软件功能复杂、数据量大,对电脑的配置要求较高。本文将从多个方面详细阐述广联达软件的电脑配置推荐,以帮助读者选择适合的电脑配置处理器广联达软件在进行建筑设计、模拟等操作时,需要进行大量的数据计算和处理,因此对处理器的要求较高。推荐选择多核心、高主频的处理器,如英特尔i7系列或AMDRyzen系列。这些处理器具有较强的计算能力和多线程处理能力,能够更好地满足广联达软件的需求。内存内存是影响计算

主板上的数字音频输出接口-SPDIF OUT主板上的数字音频输出接口-SPDIF OUTJan 14, 2024 pm 04:42 PM

主板上SPDIFOUT连接线序最近我遇到了一个问题,就是关于电线的接线顺序。我上网查了一下,有些资料说1、2、4对应的是out、+5V、接地;而另一些资料则说1、2、4对应的是out、接地、+5V。最好的办法是查看你的主板说明书,如果找不到说明书,你可以使用万用表进行测量。首先找到接地,然后就可以确定其他的接线顺序了。主板vdg怎么接线连接主板的VDG接线时,您需要将VGA连接线的一端插入显示器的VGA接口,另一端插入电脑的显卡VGA接口。请注意,不要将其插入主板的VGA接口。完成连接后,您可以

如何在Zend框架中使用ACL(Access Control List)进行权限控制如何在Zend框架中使用ACL(Access Control List)进行权限控制Jul 29, 2023 am 09:24 AM

如何在Zend框架中使用ACL(AccessControlList)进行权限控制导言:在一个Web应用程序中,权限控制是至关重要的一项功能。它可以确保用户只能访问其有权访问的页面和功能,并防止未经授权的访问。Zend框架提供了一种方便的方法来实现权限控制,即使用ACL(AccessControlList)组件。本文将介绍如何在Zend框架中使用ACL

请推荐一款性价比较高的1155针CPU请推荐一款性价比较高的1155针CPUJan 14, 2024 pm 01:30 PM

求推荐1155针的cpu哪个最好当前性能最高的1155针CPU是IntelCorei7-3770K。它拥有4个核心和8个线程,基础频率为3.5GHz,并支持TurboBoost2.0技术,最高可达到3.9GHz。此外,它还搭载了8MB的三级缓存,是一款非常出色的处理器LGA1155针最强的CPUIntel酷睿i73770K。LGA1155接口为二三代酷睿处理器所使用的接口类型,性能最好的为Intel酷睿i73770K,这款处理器参数如下:1.适用类型:台式机;2.CPU系列:酷睿i7;3.CPU

我准备去西藏旅行背包去①背多少升的包合适把你认为最好的配置说下本人170体力不错第一次我准备去西藏旅行背包去①背多少升的包合适把你认为最好的配置说下本人170体力不错第一次Jan 07, 2024 am 10:06 AM

我准备去西藏旅行背包去①背多少升的包合适把你认为最好的配置说下本人170体力不错第一次去徒步多就60升或以上的徒步少就60升以下的全程都坐车就不用背包,旅行箱更方便,真要随身带东西,弄个25~40升的就绰绰有馀西藏旅游必备用品:太阳镜、太阳帽、防晒霜、护肤霜、润唇膏、长袖上衣、毛衣;对于特殊旅游或去阿里、藏北、川藏线旅游,建议带:睡袋(防寒)、床单(防脏)、羽绒服、旅游鞋或登山鞋、拖鞋、牙刷、牙膏、毛巾、卷筒纸、纸内裤、消毒湿巾、手电筒、防水火柴、刀具、绳子。前运包能装电脑吗能装电脑,有些背包有

最佳显卡驱动版本是什么?(哪个显卡驱动版本最好用)最佳显卡驱动版本是什么?(哪个显卡驱动版本最好用)Jan 10, 2024 pm 11:13 PM

哪个版本的显卡驱动最好用1、没有绝对的最好版本,选择适合自己电脑的版本最重要;2、因为显卡驱动版本的适用性和稳定性与电脑硬件环境和系统的配置有关;3、可以在官网查看电脑和显卡的详细信息,根据信息选择适合的驱动版本,也可以参考其他用户的评价选择。建议在安装驱动之前备份系统,以免出现意外情况。显卡驱动版本472.19系列是非常出色的选择。目前,472版本的驱动兼容性是最优秀的。安装472版本的驱动也能够使显卡发挥出最佳性能。NVIDIA显卡驱动Win7安装版,编号为2、472.19,是一款质量显著的

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools