How Zend_Controller works_PHP tutorial
Zend_Controller is the basis for building a site using the MVC pattern. The Zend_Controller system is a lightweight, modular and extensible system. It only provides the core necessary parts, allowing developers a lot of freedom to flexibly build their own sites. The code structure of sites using Zend_Controller will be partially similar.
Zend_Controller’s workflow is implemented through several components. Although you don’t need to fully understand the meaning of these components, it is helpful if you have a little understanding of the workflow:
- Zend_Controller_Front front-end controller
- Zend_Controller_Router
- Zend_Controller_Dispatcher Dispatcher
- Zend_Controller_Action
Zend_Controller_Front (front-end controller) is the organizer of the Zend_Controller_Controller system. It is the implementation of the FrontController design pattern. Zend_Controller_Front handles all requests accepted by the server and is ultimately responsible for assigning the request to an ActionController (Zend_Controller_Action). Zend_Controller_Controller forwards the customer's original request to the corresponding processing component (Action) to complete specific business processing.
Zend_Controller_Router is equivalent to a router. Routing is the process of decomposing a URI and deciding which Controller and what Action to use to handle it. The definition of controller, action and optional parameters are encapsulated into an object - Zend_Controller_Dispather_Token, which is then processed by Zend_Controller_Dispatcher. Routing happens only once: when the request is received by the server, before being dispatched to the first controller. Zend_Controller_Router is responsible for parsing the customer's request URI and analyzing the parameters to determine which ActionController to target. Zend_Controller_Router will encapsulate the analyzed parameters into Zend_Controller_Dispather_Token objects.
The so-called router is very similar to the function of the network router we are familiar with. It has the function of determining the network address and selecting the path. It is used here for redirection.
The process of "allocation" is to find the appropriate controller file based on Zend_Controller_Dispatcher_Token, instantiate the controller class in it (Zend_Controller_Action must be implemented), and finally run the action in the controller. Unlike routing, the allocation process is repeated continuously. Zend_Controller_Dispatcher is continuously called by Zend_Controller_Front until all actions are allocated in sequence. Zend_Controller_Dispatcher forwards the request to the corresponding Zend_Controller_Action based on the Zend_Controller_Router's parsing result of the request URI (a Zend_Controller_Dispather_Token object).
Zend_Controller_Action is the most basic controller. Each specific controller inherits from the Zend_Controller_Action class, is a subclass of Zend_Controller_Action, and has its own action method. Zend_Controller_Action is the basic controller, which actually performs the specific processing of a user request.
The workflow of Zend_Controller is quite simple. Zend_Controller_Front receives a request, and then Zend_Controller_Router decides which controller (the class that implements Zend_Controller_Action) to assign to. Zend_Controller_Router decomposes and encapsulates the URI into a Zend_Controller_Dispatcher_Token object. Zend_Controller_Front then enters a distribution loop, calls Zend_Controller_Dispatcher, and passes the token object to the dispatcher to assign it to specific controllers and actions for processing. After the controller ends, control is transferred to Zend_Controller_Front. If the controller finds that it needs to allocate another controller (returning a new token object), the loop continues until another allocation is completed.
Zend_Controller workflow diagram is as follows:

Route Process routing process
Before you build your first controller, you need to understand how the redirection process in Zend_Controller_Router works. Remember that the workflow is divided into two steps: one is routing, which only happens once; the other is dispatching, which is a cyclic process.
Zend_Controller_Front calls Zend_Controller_Router to map a URI to a controller (Zend_Controller_Action class) and the actions in it. Zend_Controller_Router decomposes the URI, generates an object Zend_Controller_Dispatcher_Token, and then passes it to the distributor (Zend_Controller_Dispatcher).
The router uses a very simple method to determine the controller and its action to be used (URI maps to Zend_Controller_Action):
http://www.bkjia.com/controller/action/
The controller above is the controller we want to use, and the action is the action we want to use.
Other optional GET parameters can be defined in the URI and passed to the controller. The format is key/value: http://framework.zend.com/controller/action/key1/value1/
If /controller/ is not specified, index will be called by default. If /action/ is not written, index will be called by default. Such as:
http://framework.zend.com/roadmap/future/ Controller: roadmap Action : future http://framework.zend.com/roadmap/ Controller: roadmap Action : index http://framework.zend.com/ Controller: index Action : index
Controller, action name and other parameters will be encapsulated into a token object - Zend_Controller_Dispatcher_Token. This object is passed back to Zend_Controller_Front, then enters the allocation process and is passed to Zend_Controller_Dispatcher.
Token object
Token object is a very simple object, passed between Zend_Controller_Front and classes that implement router and dispatcher interfaces. It encapsulates controller, action and other GET parameters.
- The name of the controller is obtained and set through getControllerName() and setControllerName()
- The name of the action is obtained and set through getActionName() and setActionName().
- The parameters passed to the action are an array (key/value form), which can be obtained through getParams() and set through setParams().
Dispatch Process distribution process
The so-called allocation process is to extract the information from the token object (Zend_Controller_Dispatcher_Token): the name of the controller, the name of the action, parameters, etc., and then instantiate a controller and call the action in it for processing.

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 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 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

When you decide to develop an ERP system, choosing a suitable framework is crucial. Here we will compare the two PHP frameworks CodeIgniter and ZendFramework to help you find a framework that is more suitable for your ERP system development. CodeIgniter and ZendFramework are popular PHP frameworks. They all provide many features and are extensible and maintainable. However, these two frameworks differ significantly in some aspects and are more suitable for some applications.

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
