Home > Article > Backend Development > 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) 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:
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 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 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.