Zend_Controller的工作原理
Zend_Controller是使用MVC模式来构建一个站点的基础。Zend_Controller体系是一个轻量的,模块化和可扩展的体系。它只提供最核心的必要的部分,允许开发者有很大的自由来灵活地构建自己的站点。使用Zend_Controller的站点,其代码结构会有一部份是相似的。
Zend_Controller 的工作流(workflow)是通过几个组件来实现的。虽然不需要完全理解这几个组件的含义,如果你对工作流程有点了解是很有帮助的:
- Zend_Controller_Front 前端控制器
- Zend_Controller_Router
- Zend_Controller_Dispatcher 分配器
- Zend_Controller_Action
Zend_Controller_Front(前端控制器)是Zend_Controller_Controller体系的组织者,它是FrontController设计模式的实现。 Zend_Controller_Front处理服务器接受的所有请求,并最后负责将请求分配给ActionController (Zend_Controller_Action)。Zend_Controller_Controller就是将客户的原始请求转发给相应的处理组件(Action)去完成具体的业务处理。
Zend_Controller_Router相当于一个路由器。路由是将一个URI分解并决定由哪一个Controller,和用什么Action来处理的过程。controller,action和可选的参数的定义,被封装成一个对象-- Zend_Controller_Dispather_Token,然后由Zend_Controller_Dispatcher来处理。路由只发生一 次:当请求被服务器接收到时,在分配到第一个控制器之前。Zend_Controller_Router负责解析客户的请求URI,分析其中的参数,以决定定位到哪个ActionController。Zend_Controller_Router会把分析出的参数封装成Zend_Controller_Dispather_Token对象。
所谓router,和我们熟知的网络路由器的功能是很相似的,具有判断网络地址和选择路径的功能,这里就是用来重定向。
"分配"的过程即根据Zend_Controller_Dispatcher_Token来查找适合的contoller文件,实例化其中的controller类(必须实现Zend_Controller_Action),并且最后在控制器内运行action。和路由不同,分配的过程不断重复的,Zend_Controller_Dispatcher被Zend_Controller_Front不断调用直到所有action都依次分配完毕。Zend_Controller_Dispatcher是根据Zend_Controller_Router对请求URI的解析结果(一个Zend_Controller_Dispather_Token对象)将请求转发给相应的Zend_Controller_Action。
Zend_Controller_Action是最基本的控制器。每个具体的控制器都是从Zend_Controller_Action类继承而来,是Zend_Controller_Action的子类,并且有自己的action方法。Zend_Controller_Action是基本的控制器,真正对一次用户请求进行具体处理的工作是由它来完成的。
Zend_Controller的工作流程相当简单。Zend_Controller_Front接收一个请求,然后由 Zend_Controller_Router来决定分配给哪个controller(实现Zend_Controller_Action的类)。Zend_Controller_Router把URI分解封装成 一个Zend_Controller_Dispatcher_Token 对象。Zend_Controller_Front接着进入一个分配循环,调用Zend_Controller_Dispatcher,把token对象 传给dispatcher来分配到具体的controller和action进行处理。在controller结束后,又把控制权交加给 Zend_Controller_Front。如果controller发现需要分配另一个controller(返回一个新的token对象),循环会一直继续直到另一次分配执行完毕。
Zend_Controller工作流示意图如下:
Route Process 路由过程
在你构建第一个控制器之前,你需要理解Zend_Controller_Router中的重定向过程是如何工作的。记住工作流程分为两步:一是重定向(routing),只发生一次;二是分配(dispatching),循环过程。
Zend_Controller_Front 调用Zend_Controller_Router来使一个URI 映射到一个controller(Zend_Controller_Action类)及其中的action上。 Zend_Controller_Router分解URI,生成一个对象Zend_Controller_Dispatcher_Token,然后再传递给分配器(Zend_Controller_Dispatcher)。
router使用很简单的方法来决定所要使用的controller及其action(URI映射到Zend_Controller_Action):
http://www.bkjia.com/controller/action/
上面controller就是我们要采用的控制器,action就是我们要采用的action。
其它可选择的GET参数可以在URI中定义,并传递给controller。格式为key/value:http://framework.zend.com/controller/action/key1/value1/
如果/controller/这部分没有写明,默认调用index。 如果/action/这部份没有写,也默认调用index。如:
1 2 3 4 5 6 7 8 9 |
|
controller,action的名称及其它参数将会被封装到一个token对象中-- Zend_Controller_Dispatcher_Token。这个对象被传回到Zend_Controller_Front,然后进入分配过程, 被传递给Zend_Controller_Dispatcher。
Token对象
Token对象是个很简单的对象,在Zend_Controller_Front和实现router,dispatcher接口的类间传递。它把controller,action和其它GET参数封装在一起。
- controller的名称通过getControllerName()及setControllerName()来获得和设置
- action的名称过getActionName()和setActionName()来获得和设置。
- 传递给action的参数是个数组(key/value形式),可以通过getParams()来获得,通过setParams()来设置。
Dispatch Process 分配过程
所谓分配过程,就是根据token对象(Zend_Controller_Dispatcher_Token) ,抽取其中的信息:controller的名称,action的名称,参数等等,然后实例化一个controller,并调用其中的action来进行处理。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Using ZendFramework with PHP: Quick Start Guide ZendFramework is an open source, PHP-based web application framework that is powerful and easily extensible. ZendFramework contains many useful components that can help you build efficient web applications. This article will introduce how to use ZendFramework in PHP to help you get started quickly. Install ZendFramewo

Implementing efficient database queries through ZendFramework middleware Introduction In the development process, database queries are an inevitable part. An efficient database query can greatly improve system performance and user experience. ZendFramework is a widely used PHP framework with powerful database operation functions. This article will introduce how to implement efficient database queries through ZendFramework middleware and provide corresponding code examples. 1. Understanding ZendF

ZendFramework is a powerful development framework that helps developers quickly build high-performance, scalable PHP applications. Among them, middleware is an important concept in ZendFramework, which can help us implement full-text search and paging functions. This article will introduce how to use middleware in ZendFramework to implement these two functions and provide code examples. 1. Full-text search function Full-text search is one of the common functions in modern applications.

ZendFramework Middleware: Adding OAuth and OpenID Login Support to Applications User authentication is a critical feature in today's Internet applications. In order to provide better user experience and security, many applications choose to integrate third-party login services, such as OAuth and OpenID. In ZendFramework, we can easily add OAuth and OpenID login support to our applications through middleware. First, we need to install Ze

ZendFramework is an open source framework based on PHP that provides many powerful tools and components for building scalable web applications. This article will introduce how to use ZendFramework's middleware to add social login functionality to web applications. Middleware is code that is executed before or after a request enters your application. It allows developers to customize and extend the process of handling requests. ZendFramework provides a flexible way to

ZendFramework middleware: Provides email notification and message push functions Introduction: With the development of the Internet and the popularity of smartphones, email notification and message push have become one of the commonly used functions in modern software development. In ZendFramework, we can use middleware to implement email notification and message push functions. This article will introduce how to use ZendFramework middleware to implement email notifications and message push, and provide corresponding code examples. 1. Preparation work in

ZendFramework middleware: Adding Alipay and WeChat payment functions to applications Introduction: With the popularity of mobile payments, Alipay and WeChat payment have become essential payment methods in many applications. This article will introduce how to use ZendFramework middleware to add Alipay and WeChat payment functions to the application. By studying this article, you will learn how to use middleware to simplify the payment process and apply it to your actual projects. 1. Preparation Before starting, you

ZendFramework2 is a popular PHP programming framework that provides a wealth of functions and modules, allowing PHP developers to build high-quality Web applications more conveniently. This article will introduce some common ZendFramework2 operations to help you better use this framework. MVC pattern In ZendFramework2, the Model-View-Controller (MVC) pattern is the most common architecture. The MVC pattern is a
