Zend Framework Router Usage example detailed _php instance

Source: Internet
Author: User
Tags regular expression zend zend framework

This example describes the Zend Framework router usage. Share to everyone for your reference, specific as follows:

A route is a process in which it removes the endpoint of the URI (followed by the URI portion of the base URL) and decomposes 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 routers

In order to use the router correctly, it must be initialized.

The creation of a router can be accomplished by the Getrouter () method of the front-end controller instance. This method does not require any parameters, and the method can be executed to return a Zend_controller_router_rewrite object.

After creating the router, you need to add some user-defined routes that can be implemented by the Addroute () method of the Zend_controller_router_rewrite object.

Code:

<?php
/**
demonstrates the process of creating a router
* *
require_once ' zend/controller/front.php ';    Reference zend_controller_front.php
$ctrl = Zend_controller_front::getinstance ();  Create a front-end controller
$router = $ctrl->getrouter ();          Returns a default route, and the front-end controller is powerful.
$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 that is stored in Rewriterouter named ' Default '.

2. Standard frame Routing

Definition: Zend_controller_router_route is a standard framework route.

Example:

<?php
//define standard frame 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 dizzy, the log is not good code ah, I do not understand.

3. Static routing

Definition: A specific route is set to shape zend_controller_router_route_static.

Example:

<?php
//define static routing
$route = new Zend_controller_router_route_static ('
login ',
array (
  ' Controller ' => ' auth ',
  ' action ' => ' login ')
;
Adds a defined route to the router
$router->addroute (' login ', $route);

The above route matches the Http://domain.com/login URL and assigns 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);

Analysis:

The dynamic portion of the first parameter of the regular expression route definition (the content after "/") is no longer a variable, but rather a regular child mode.

In this example, after the http://domain.com/archive/2008 is successfully matched, an array of the following result values is returned.

$values = Array (
1=> ' 2008 ', '
controller ' => ' archive ',
' action ' => ' show '
);

Postscript:

I mean I have too many concepts and I have a lot of difficulty.

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.