This article describes the usage of Zend Framework router. Share it for your reference, as follows:
Routing is a process in which it removes the endpoint of the URI (following the URI part of the base URL) and breaks it into parameters to determine which module, which controller and which action should accept the request.
Modules, controllers, actions, and their parameters are packaged into the Zend_Controller_Request_Http object.
Using a router
In order to use the router correctly, it must be initialized.
Creating a router can be achieved through the getRouter() method of the front-end controller instance. This method does not require any parameters, and executing this method can return a Zend_Controller_Router_Rewrite object.
After creating a router, you need to add some user-defined routes, which can be implemented through the addRoute() method of the Zend_Controller_Router_Rewrite object.
Code:
<?php /** Demonstrate the process of creating a router */ require_once 'Zend/Controller/'; //Cite Zend_Controller_Front.php$ctrl = Zend_Controller_Front::getInstance(); //Create a front-end controller$router = $ctrl->getRouter(); //Return a default route, the front-end controller has very powerful functions$router->addRoute('user',new Zend_Controller_Router_Route('user/:username',array('controller'=>'user','action'=>'info')));
4 basic routes
1. Default routing
Definition: The default route is a simple Zend_Controller_Router_Route_Module object named 'default' stored in the RewriteRouter.
2. Standard framework routing
Definition: Zend_Controller_Router_Route is a standard framework routing.
example:
<?php //Define standard framework routing$route = new Zend_Controller_Router_Route('author/:username', array( 'controller'=>'profile', 'action'=>'userinfo' )); //Add a defined route to the router$router->addRoute('user',$route);
Note: I said I was very dizzy and the logs were not easy to code, and I didn’t understand them very much.
3. Static routing
Definition: The specific route is set to form Zend_Controller_Router_Route_Static.
example:
<?php //Define static route$route = new Zend_Controller_Router_Route_Static( 'login', array( 'controller'=>'auth', 'action'=>'login' )); //Add a defined route to the router$router->addRoute('login',$route);
The above route will match the URL of /login and dispatch it to the AuthController::loginAction() method.
4. Regular expression routing
Zend_Controller_Router_Route_Regex
Case:
<?php // Regular expression routing$route = new Zend_Controller_Router_Route_Regex( 'archive/(\d+)', array( 'controller'=>'archive', 'action'=>'show' )); //Add a defined route to the router$router->addRoute('archive',$route);
analyze:
The dynamic part (the content after "/") in the first parameter defined by the regular expression route is no longer a variable, but a regular sub-pattern.
In this example, after successfully matching /archive/2008, the following array of result values will be returned.
$values = array( 1=>'2008', 'controller'=>'archive', 'action'=>'show' );
postscript:
I said there are too many concepts and it is very difficult.
For more information about Zend, please visit the special topic of this site:Zend FrameWork Framework Introduction Tutorial》、《Summary of excellent development framework for php》、《Yii framework introduction and common techniques summary》、《ThinkPHP Introduction Tutorial》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope that this article will be helpful to everyone's PHP programming based on the Zend Framework framework.