The mechanism of router in cakephp and how to use it

Source: Internet
Author: User
The mechanism and use of router in cakephp

A typical cakephp process for handling Request


?? ?? When a person browses a website, makes a request, the. htaccess file maps the request to a webroot/index.php file, which mainly deals with: 1. Definitions of constants; 2. Include the bootstrap.php file; 3. Call the Dispather () function.

?

Ii. Understanding the relationship between Router and Dispatcher and Controller

?? ? ? (1). Routing (Router) interprets and extracts the parameters of this request from the URL: Controller, action, and any other parameter information associated with the business logic that is affected in the request;

?? ? ? (2). After using routing, the requested URL is mapped to the action of a controller (a method in a particular controller class)


?? Partial function Parsing * * * *

?? 1. Router::parse ($url)

?? ? ? The corresponding controller, action, and params arrays are parsed according to the URL provided. Guidelines based on: default routes, routing rules created by the new definition.

?

?

?? 2. Router::getargs ($args, $options = Array ()) converts the passed params to a parameter

?

Iii. router configuration in CakePHP

?? ? CakePHP router.php files in the source package:



?? Custom routes:

The three parameters of the Connect () function: 1. The URL value that you want to match; 2. The default value of the custom route element; 3. A regular rule that helps the router match the URL element.

The basic format is used for router definitions:

?

Router::connect ('    URL ',    array (' paramname ' = ' defaultvalue '),    array (' paramname ' = ' Matchingregex '));

?

?

?? Note:

(1) define the URL type. URLs are usually separated by slashes, but also contain wildcards (*) or custom route elements (URL The element is prefixed with a colon , and wildcard characters are used to match the type of the URL. A specialized routing element allows you to collect parameters for your controller actions .

(2) When a request occurs, the values of these routing elements are present in the controller $this->params . This differs from named parameters (named parameter), so note the difference: named arguments (/controller/action/name:value) exist in the custom $this->passedArgs route element exists in the $this->params

? (3) default route:/:controller,/:controller/:action/*

?

?? What will this configuration return???

?? ? Write two examples to take a look:


?

Iv. application of router in CakePHP

?? (1). Simple application:


?? (2). Advanced Applications:




?? ? ? Slugroute The parsing method is simple. First we call the parent class parse () function to get the path to parse the regular expression. Any null or false return will cause our parse path to fail ( path mismatch )only if we get a valid path (for Post with matching parameters) . since Cakeroute:parse () will have created the parameters , so we don't need to modify the parameters , just returns them if no target exists or returns an error.

?? ? ? in the app/config/routes.php reference this Router class:

?

V. The drawbacks of router configuration file in CakePHP

?? ? ? CakePHP 's default implementation, we do not use to set the Route, the system will follow the /controller/action/param To parse the URL in the form . But a lot of times such URLs aren't friendly, so we're going to set up a custom route, and the result is the route The configuration file is constantly expanding. It's worth noting that PHP rebuilds the runtime environment for each request, and for one request, The last use of the route configuration file is just one configuration, for this configuration, It is a huge burden to have to repeat parsing the entire large Route configuration file.

?? ? ? Rest style is a good way to solve this problem, in the form of restful architecture, the URL of the resource is relatively determined, the mode of operation by the HTTP method, so the Route configuration file is quickly slimming.



?? ? ?? REST software Architecture is an abstract concept and a guide to the implementation of this Internet hypermedia distributed system. This concept can be achieved with any technology. The most notable thing about implementing this software architecture is the HTTP protocol. Usually we write rest as rest/http, and in practice we tend to interpret rest as the rest Software Architecture of HTTP, or even further the rest and http See as the concept of equivalence.

  • Related Article

    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.