ZF rewrite routed post-$this->url problem _php Tutorial

Source: Internet
Author: User
Tags ruby on rails
The Zend_controller_router_rewrite is a standard framework router. Routing is a process in which it takes out the endpoint of the URI (the part of the URI of the base URL) and breaks it down into parameters to determine which module, which controller, and which action in the Controller should accept the request. Modules, controllers, actions, and other parameters are packaged into the Zend_controller_request_http object, which is then handled by Zend_controller_dispatcher_standard. Routing occurs only once: When a request is initially received and the first controller is dispatched.

Zend_controller_router_rewrite is designed to consider the functionality of Mod_rewrite-like when using a pure PHP architecture. It is very loosely based on the knowledge of Ruby on rails and does not require any previous Web server URL rewriting. It is designed to work with a single Apache mod_rewrite rule (one of them):

Rewriteengine Onrewriterule!\. (JS|ICO|GIF|JPG|PNG|CSS) $ index.php

Or:

Using routers

To properly use the rewrite router you must initialize it, add some user-defined routes and inject them into the controller. The following code example is the process:

 
  Getrouter (); Returns a rewrite router by default$router->addroute (' user ', new Zend_controller_router_route (' User/:username ', Array (' Controller ' = ' user ', ' action ' = ' info '));

The core of Rewriterouter is the definition of a user-defined route. The route is added by calling Rewriterouter's Addroute method and passing a new instance of the Zend_controller_router_route_interface implemented by the class. For example:

 
  Addroute (' user ', new Zend_controller_router_route (' User/:username '));

The Rewrite router has four basic types of routes (one of which is special):

    • Zend_controller_router_route
    • Zend_controller_router_route_static
    • Zend_controller_router_route_regex
    • Default route

Routes can be used countless times to create chains or user-defined application routing plans. You can use any number of routes in any configuration, except for module routing, which is best used once and as a generic route (for example, as default). Each route is described in detail later.

The first parameter of the Addroute is the route name. It is used as authority to get a route from the router (for example, for URL generation purposes). The second parameter is to route itself. The most common use of route names is through the Zend_view_url helper method:

URL (Array (' username ' = ' martel '), ' user ')?> ">martel

Routing is a simple process that iterates through all the provided routes and URI definitions that match its current request. When a positive match is found, the value of the variable is returned from the routing instance and injected into the Zend_controller_request object for future use in the dispatcher and user-created controller. If it is a negative match, the next route in the chain is checked. The value returned from the route comes from the URL parameter or the default value that is used for the definition. These variables can be accessed later by the Zend_controller_request::getparam () or Zend_controller_action::_getparam () method.

There are three special variables available for your route-' module ', ' Controller ' and ' action '. These special variables are zend_controller_dispatcher used to locate the controller and the action and dispatch the past.

The Zend_controller_router_rewrite is preconfigured with the default route, which will match URIs in the form of controller/action. In addition, the module name can be specified as the first path parameter, allowing this module/controller/action form of URIs. Finally, it will also default to match any additional parameter appended to the URI-controller/action/var1/value1/var2/value2.

Examples of how some routes match:

Assuming the following: $ctrl->setcontrollerdirectory (        ' default ' = '/path/to/default/ Controllers ',        ' news '    = '/path/to/news/controllers ',        ' blog '    = '/path/to/blog/controllers '    )); Module Only:http://example/news    module = = Newsinvalid module maps to controller Name:http://example/foo    Controller = = Foomodule + controller:http://example/blog/archive    module     = = Blog    Controller = = Archivemodule + controller + action:http://example/blog/archive/list    module     = = Blog    Controller = = Archive    Action     = = Listmodule + controller + action + params:http://example/blog/archive/list/sort/alpha/ Date/desc    Module     = = Blog    Controller = = Archive    Action     = = List    sort       = Alpha    date       = = Desc

The default route is a simple Zend_controller_router_route_module object that is stored in the Rewriterouter name (index) as ' default '. It is created somewhat like this:

 
  Addroute (' Default ', $compat);

If you don't want this particular default route in your routing plan, you can rewrite your own ' default ' route (for example, store it under ' Default ') or completely erase it with removedefaultroutes ():

 
  Removedefaultroutes ();

Zend_controller_router_route

The Zend_controller_router_route is a standard framework route. It combines the ease of use of flexible routing definitions. Each route contains basic URL mappings (static and dynamic parts (variables)) and can be initialized by default, or it can be initialized according to different requirements.

Let's imagine that our hypothetical application will need information pages for some wide-area content authors. We want to be able to point the browser to Http://domain.com/author/martel to see a message called "Martel". The route that has such a feature looks like this:

 
  ' Profile ',        ' action '     = ' userinfo ')    ; $router->addroute (' user ', $route);

In Zend_controller_router_route the first parameter of the constructor is the definition of the route, which will match a URL. The route definition contains both static and dynamic parts, separated by a forward slash ('/') character. The static part is just a simple character: author. The dynamic part, called the variable, uses a pre-defined colon to mark the variable name: username.

When you point the browser to Http://domain.com/author/martel This example of the route should be matched, all of its variables will be injected into the Zend_controller_request object and accessible in Profilecontroller. The variable returned by this example may be represented as an array of the following key and value pairs:

 
  ' Martel ',    ' controller ' = ' profile ',    ' action '     = ' userinfo ');

Later, based on these values, Zend_controller_dispatcher_standard should call the Userinfoaction () method in the Profilecontroller class (in the default module). You will rely on the Zend_controller_action::_getparam () or Zend_controller_request::getparam () method to access all variables:

 
  Getrequest ();    $username = $request->getparam (' username ');    $username = $this->_getparam (' username ');}

The route definition can package an extra special character-a wildcard-represented as the ' * ' sign. It is used to obtain parameters that are similar to the default module route (Var=>value defined in the URI). The following route mimics the behavior of the module route in some way:

 
  ' Default '); $router->addroute (' Default ', $route);

Variable request

When a variable request is set, the third parameter can be added to the Zend_controller_router_route constructor. These are defined as part of the regular expression:

 
  2006,        ' controller ' = ' archive ',        ' action '     = ' show '    ),    Array (' year ' = ' = ' \d+ ')); Router->addroute (' archive ', $route);

With the routes defined above, the router will match it only if the year variable contains numeric data, such as http://domain.com/archive/2345. URLs like Http://example.com/archive/test will not be matched and control will be passed to the next route in the chain.

A simple example of application

 
  
  
  • URL (Array (' controller ' = = ' index '), null,true); > "title=" Home > Homepage
  • URL (Array (' controller ' = ' news ', ' action ' = ' index '), null,true); > "title=" News > Press

http://www.bkjia.com/PHPjc/752425.html www.bkjia.com true http://www.bkjia.com/PHPjc/752425.html techarticle the Zend_controller_router_rewrite is a standard framework router. A route is a process in which it takes out the endpoint of the URI (the part of the URI that follows the base URL) and breaks it down into parameters ...

  • 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.