Home > php教程 > PHP开发 > body text

Detailed explanation of Zend Framework router usage examples

高洛峰
Release: 2017-01-06 09:48:04
Original
1214 people have browsed it

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 &#39;Zend/Controller/Front.php&#39;;    //引用Zend_Controller_Front.php
$ctrl = Zend_Controller_Front::getInstance();  //创建一个前端控制器
$router = $ctrl->getRouter();          //返回一个默认路由,前端控制器功能很强大啊
$router->addRoute(&#39;user&#39;,new Zend_Controller_Router_Route(&#39;user/:username&#39;,array(&#39;controller&#39;=>&#39;user&#39;,&#39;action&#39;=>&#39;info&#39;)));
Copy after login

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(&#39;author/:username&#39;,
array(
  &#39;controller&#39;=>&#39;profile&#39;,
  &#39;action&#39;=>&#39;userinfo&#39;
));
//向路由器中添加定义的路由
$router->addRoute(&#39;user&#39;,$route);
Copy after login

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(
&#39;login&#39;,
array(
  &#39;controller&#39;=>&#39;auth&#39;,
  &#39;action&#39;=>&#39;login&#39;
));
//向路由器中添加定义的路由
$router->addRoute(&#39;login&#39;,$route);
Copy after login

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(
  &#39;archive/(\d+)&#39;,
  array(
    &#39;controller&#39;=>&#39;archive&#39;,
    &#39;action&#39;=>&#39;show&#39;
  ));
//向路由器中添加定义的路由
$router->addRoute(&#39;archive&#39;,$route);
Copy after login

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=>&#39;2008&#39;,
&#39;controller&#39;=>&#39;archive&#39;,
&#39;action&#39;=>&#39;show&#39;
);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!