Zend_Controller is the basis for building a site using the MVC mode. Zend_Controller is a lightweight, modular, and scalable system. It only provides the core necessary parts, allowing developers to freely build their own sites. Some of the code structures of sites using Zend_Controller are similar.
The workflow of Zend_Controller is implemented by several components. Although you do not need to fully understand the meanings of these components, it is helpful to know a little about the workflow:
- Zend_Controller_Front front-end Controller
Zend_Controller_Front (front-end Controller) is the organizer of the Zend_Controller_Controller system. It is the implementation of the FrontController design mode. Zend_Controller_Front processes all requests received by the server, and is responsible for allocating the requests to ActionController (Zend_Controller_Action ). Zend_Controller_Controller is to forward the customer's original request to the corresponding processing component (Action) to complete specific business processing.
- Zend_Controller_Router
Zend_Controller_Router is equivalent to a vro. A route is a process of decomposing a URI and deciding which Controller to use and what Action to process it. The controller, action, and optional parameter definitions are encapsulated into an object -- Zend_Controller_Dispather_Token, which is then processed by Zend_Controller_Dispatcher. A route occurs only once: when a request is received by the server, it is allocated to the first controller. Zend_Controller_Router parses the customer's request URI and analyzes the parameters to determine which ActionController to locate. Zend_Controller_Router encapsulates the analyzed parameters into a Zend_Controller_Dispather_Token object.
The so-called router is very similar to the functions of the well-known network router. It has the function of judging the network address and selecting the path. This is used for redirection.
- Zend_Controller_Dispatcher distributor
The "Allocation" process is to find the appropriate contoler File Based on Zend_Controller_Dispatcher_Token, instantiate the controller class (Zend_Controller_Action must be implemented), and finally run the action in the controller. Different from routing, the allocation process is repeated. Zend_Controller_Dispatcher is called by Zend_Controller_Front until all actions are allocated in sequence. Zend_Controller_Dispatcher forwards the request to the corresponding zend_controller_controller_rocher Based on the parsing result of the request URI (A Zend_Controller_Dispather_Token object.
- Zend_Controller_Action
Zend_Controller_Action is the most basic controller. Each specific controller is inherited from the Zend_Controller_Action class. It is a subclass of Zend_Controller_Action and has its own action method. Zend_Controller_Action is a basic controller. It is used to process a user request.
The Zend_Controller workflow is quite simple. Zend_Controller_Front receives a request, and then Zend_Controller_Router determines which controller to allocate to (class that implements Zend_Controller_Action ). Zend_Controller_Router splits the URI into a Zend_Controller_Dispatcher_Token object. Zend_Controller_Front then enters a allocation cycle, calls Zend_Controller_Dispatcher, and passes the token object to the dispatcher to assign it to the specific controller and action for processing. After the controller ends, the control is handed over to Zend_Controller_Front. If the controller finds that another controller needs to be allocated (a new token object is returned), the loop continues until another allocation is completed.
The Zend_Controller workflow is as follows:
Route Process routing Process
Before you build the first controller, you need to understand how the redirection process in Zend_Controller_Router works. Remember that a workflow can be divided into two steps: one is redirection (routing), which only occurs once; the other is dispatching and the loop process.
Zend_Controller_Front calls Zend_Controller_Router to map a URI to a controller (Zend_Controller_Action class) and its actions. Zend_Controller_Router breaks down the URI, generates an object Zend_Controller_Dispatcher_Token, and passes it to the distributor (Zend_Controller_Dispatcher ).
Router uses a simple method to determine the controller to be used and its action (URI ing to Zend_Controller_Action ):
Http://www.bkjia.com/controller/action/
The controller above is the controller we want to adopt, and action is the action we want to adopt.
Other optional GET parameters can be defined in the URI and passed to the controller. Format: key/value: http://framework.zend.com/controller/action/key1/value1/
If/controller/is not specified, index is called by default. If/action/is not written, index is also called by default. For example:
http://framework.zend.com/roadmap/future/Controller: roadmapAction : futurehttp://framework.zend.com/roadmap/Controller: roadmapAction : indexhttp://framework.zend.com/Controller: indexAction : index
The 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, and then enters the allocation process and is passed to Zend_Controller_Dispatcher.
Token Object
The Token object is a very simple object. It is passed between Zend_Controller_Front and classes that implement the router and dispatcher interfaces. It encapsulates controller, action, and other GET parameters.
- The controller name is obtained and set through getControllerName () and setControllerName ().
- The action name is obtained and set through getActionName () and setActionName.
- The parameter passed to action is an array (in the form of key/value), which can be obtained through getParams () and set through setParams.
Dispatch Process allocation Process
The so-called allocation process is to extract information from the token object (Zend_Controller_Dispatcher_Token): controller name, action name, parameter, and so on, and then instantiate a controller, and call the action to process it.