Home Backend Development PHP Tutorial Zend的MVC机制使用分析(一)_PHP

Zend的MVC机制使用分析(一)_PHP

Jun 01, 2016 pm 12:06 PM
mvc zend

代码
复制代码 代码如下:
$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,可以看到,这里是作为一个插件的形式放进来的。

接着又获取了Zend_Controller_Front的实例,调用:

$front->registerPlugin(

new $pluginClass($this),

99

);

 

记得前面对Zend_Controller_Front的分析不?里面有registerPlugin的函数,是将插件委托给front的broker来用。有人就会问后面的99是什么意思?是插件的索引顺序,越后面的插件越后执行插件的动作。

 

下面再看_initHelper:

获取helperClass,这里的helperClass就是Zend_Layout_Controller_Action_Helper_Layout

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

。。。

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

}

如果Action_HelperBroker没有layout的helper的话

就执行下面的offsetSet命令。将-90和Zend_Layout_Controller_Action_Helper_Layout实例作为参数传入。

和plugin同样的关系,将Zend_Layout_Controller_Action_Helper_Layout实例作为value存入到this->_helpersByPriority和this->_helpersByNameRef去了

前面的-90是权重,也是要保证这个helper是最后调用(看最后一行是krsort排序)

好了,Layout的构造函数就这样分析结束了。

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP MVC Architecture: Building Web Applications for the Future PHP MVC Architecture: Building Web Applications for the Future Mar 03, 2024 am 09:01 AM

Introduction In today's rapidly evolving digital world, it is crucial to build robust, flexible and maintainable WEB applications. The PHPmvc architecture provides an ideal solution to achieve this goal. MVC (Model-View-Controller) is a widely used design pattern that separates various aspects of an application into independent components. The foundation of MVC architecture The core principle of MVC architecture is separation of concerns: Model: encapsulates the data and business logic of the application. View: Responsible for presenting data and handling user interaction. Controller: Coordinates the interaction between models and views, manages user requests and business logic. PHPMVC Architecture The phpMVC architecture follows the traditional MVC pattern, but also introduces language-specific features. The following is PHPMVC

An advanced guide to PHP MVC architecture: unlocking advanced features An advanced guide to PHP MVC architecture: unlocking advanced features Mar 03, 2024 am 09:23 AM

The MVC architecture (Model-View-Controller) is one of the most popular patterns in PHP development because it provides a clear structure for organizing code and simplifying the development of WEB applications. While basic MVC principles are sufficient for most web applications, it has some limitations for applications that need to handle complex data or implement advanced functionality. Separating the model layer Separating the model layer is a common technique in advanced MVC architecture. It involves breaking down a model class into smaller subclasses, each focusing on a specific functionality. For example, for an e-commerce application, you might break down the main model class into an order model, a product model, and a customer model. This separation helps improve code maintainability and reusability. Use dependency injection

PHP Implementation Framework: Zend Framework Getting Started Tutorial PHP Implementation Framework: Zend Framework Getting Started Tutorial Jun 19, 2023 am 08:09 AM

PHP implementation framework: ZendFramework introductory tutorial ZendFramework is an open source website framework developed by PHP and is currently maintained by ZendTechnologies. ZendFramework adopts the MVC design pattern and provides a series of reusable code libraries to serve the implementation of Web2.0 applications and Web Serve. ZendFramework is very popular and respected by PHP developers and has a wide range of

How to use ACL (Access Control List) for permission control in Zend Framework How to use ACL (Access Control List) for permission control in Zend Framework Jul 29, 2023 am 09:24 AM

How to use ACL (AccessControlList) for permission control in Zend Framework Introduction: In a web application, permission control is a crucial function. It ensures that users can only access the pages and features they are authorized to access and prevents unauthorized access. The Zend framework provides a convenient way to implement permission control, using the ACL (AccessControlList) component. This article will introduce how to use ACL in Zend Framework

Uncovering the success of the SpringMVC framework: why it is so popular Uncovering the success of the SpringMVC framework: why it is so popular Jan 24, 2024 am 08:39 AM

SpringMVC framework decrypted: Why is it so popular, specific code examples are needed Introduction: In today's software development field, the SpringMVC framework has become a very popular choice among developers. It is a Web framework based on the MVC architecture pattern, providing a flexible, lightweight, and efficient development method. This article will delve into the charm of the SpringMVC framework and demonstrate its power through specific code examples. 1. Advantages of SpringMVC framework Flexible configuration method Spr

How to implement the MVC pattern using PHP How to implement the MVC pattern using PHP Jun 07, 2023 pm 03:40 PM

The MVC (Model-View-Controller) pattern is a commonly used software design pattern that can help developers better organize and manage code. The MVC pattern divides the application into three parts: Model, View and Controller, each part has its own role and responsibilities. In this article, we will discuss how to implement the MVC pattern using PHP. Model A model represents an application's data and data processing. usually,

How to implement scalable MVC architecture in PHP8 framework How to implement scalable MVC architecture in PHP8 framework Sep 11, 2023 pm 01:27 PM

How to implement a scalable MVC architecture in the PHP8 framework Introduction: With the rapid development of the Internet, more and more websites and applications adopt the MVC (Model-View-Controller) architecture pattern. The main goal of MVC architecture is to separate different parts of the application in order to improve the maintainability and scalability of the code. In this article, we will introduce how to implement a scalable MVC architecture in the PHP8 framework. 1. Understand the MVC architecture pattern. The MVC architecture pattern is a software design

How to use MVC architecture to design projects in PHP How to use MVC architecture to design projects in PHP Jun 27, 2023 pm 12:18 PM

In Web development, MVC (Model-View-Controller) is a commonly used architectural pattern for processing and managing an application's data, user interface, and control logic. As a popular web development language, PHP can also use the MVC architecture to design and build web applications. This article will introduce how to use MVC architecture to design projects in PHP, and explain its advantages and precautions. What is MVCMVC is a software architecture pattern commonly used in web applications. MV

See all articles