The examples in this article describe the usage of Zend Framework router. Sharing it with you for your reference, the details are 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 decide which module, Which controller and which action should accept the request.
Modules, controllers, actions, and their parameters are packaged into Zend_Controller_Request_Http objects.
Using the 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 the router, you need to add some user-defined routes. This operation can be achieved through the addRoute() method of the Zend_Controller_Router_Rewrite object.
Code:
<?php /** 演示创建路由器的过程 */ require_once 'Zend/Controller/Front.php'; //引用Zend_Controller_Front.php $ctrl = Zend_Controller_Front::getInstance(); //创建一个前端控制器 $router = $ctrl->getRouter(); //返回一个默认路由,前端控制器功能很强大啊 $router->addRoute('user',new Zend_Controller_Router_Route('user/:username',array('controller'=>'user','action'=>'info')));
4 basic routes
1. Default route
Definition: The default route is a simple Zend_Controller_Router_Route_Module object named 'default' stored in RewriteRouter.
2. Standard framework routing
Definition: Zend_Controller_Router_Route is a standard framework routing.
Example:
<?php //定义标准框架路由 $route = new Zend_Controller_Router_Route('author/:username', array( 'controller'=>'profile', 'action'=>'userinfo' )); //向路由器中添加定义的路由 $router->addRoute('user',$route);
Note: I said that I was dizzy, the log is not easy to code, and I don’t understand it well.
3. Static routing
Definition: A specific route is set to form Zend_Controller_Router_Route_Static.
Example:
<?php //定义静态路由 $route = new Zend_Controller_Router_Route_Static( 'login', array( 'controller'=>'auth', 'action'=>'login' )); //向路由器中添加定义的路由 $router->addRoute('login',$route);
The above route will match the URL of http://domain.com/login and dispatch it to the AuthController: :loginAction() method.
4. Regular expression routing
Zend_Controller_Router_Route_Regex
Case:
<?php //正则表达式路由 $route = new Zend_Controller_Router_Route_Regex( 'archive/(\d+)', array( 'controller'=>'archive', 'action'=>'show' )); //向路由器中添加定义的路由 $router->addRoute('archive',$route);
Analysis:
The dynamic part (content after "/") in the first parameter of the regular expression routing definition is no longer a variable, but a regular sub-pattern.
In this example, after successfully matching http://domain.com/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.
I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.
For more detailed examples of Zend Framework router usage and related articles, please pay attention to the PHP Chinese website!