Mechanism and usage of the Router in CakePHP

Source: Internet
Author: User
Tags constant definition
Mechanism and usage of the Router in CakePHP I. typical CakePHP Request processing process ???? When a person browses the website and sends a request ,. the htaccess file maps the request to webroot/index. php file, which is mainly used for processing: 1. constant definition; 2. include bootstrap. php file; 3. call the Router mechanism and usage in d CakePHP

I. typical CakePHP Request processing process


?? ? ? When a person browses the website and sends a request ,. the htaccess file maps the request to webroot/index. php file, which is mainly used for processing: 1. constant definition; 2. include bootstrap. php file; 3. call the dispather () function.

?

2. Understanding the relationship between Router, Dispatcher, and Controller

?? ? ? (1 ). the Router explains from the URL and extracts the request parameters: controller and action) and all other parameter information related to the business logic in this request;

?? ? ? (2). after routing is used, the requested URL is mapped to the action of a controller (a method is obtained for a specific controller)


?? * *** Part of function parsing ****

?? 1. Router: parse ($ url)

?? ? ? Parse the corresponding arrays of controller, action, and params based on the provided url. Rules: default routes and new routing rules.

?

?

?? 2. Router: getArgs ($ args, $ options = array () converts passed params to parameters

?

3. configure the Router in CakePHP

?? ? The router. php file in the CakePHP source code package:



?? Custom route:

The connect () function has three parameters: 1. the URL value you want to match; 2. default value of custom route elements; 3. helps the router match the regular rules of URL elements.

The basic format is used for Router definition:

?

Router::connect(    'URL',    array('paramName' => 'defaultValue'),    array('paramName' => 'matchingRegex'));

?

?

?? Note:

(1) define the URL type. A url is usually divided by a slash, but it also contains some wildcards (*) or custom routing elements (the URL element is prefixed with a colon) and matches the URL type with wildcards. The special routing element allows you to collect parameters for your controller actions.

(2) when a request appears, the value of these routing elements will be in$this->paramsYes. This is different from the nameparameter (named parameter), so note the difference: the nameparameter (/controller/action/name: value) exists in$this->passedArgsAnd the custom route elements exist in$this->paramsMedium

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

?

?? ? What will this configuration return ???

?? ? Let's take a look at two examples:


?

4. Application of Router in CakePHP

?? (1). simple application:


?? (2). advanced applications:




?? ? ? The parsing method of SlugRoute is very simple. First, call the parse () function of the parent class to obtain the path for parsing the regular expression. Any null or false results in parsing path failure (path mismatch), only when we get a valid path (with matched parameters for post ). since CakeRoute: parse () will have already created parameters, so we don't need to modify the parameters and will return them if there is no target or an error is returned.

?? ? ? Inapp/config/routes.phpMediumReference this router class:

?

5. disadvantages of the Router configuration file in CakePHP

?? ? ? In the default implementation of CakePHP, we do not need to set the Route. The system will follow the/controller/action/param format to parse the URL. However, many times such URLs are unfriendly, so we need to set a custom Route. The result is that the Route configuration file continues to expand. It is worth noting that PHP re-creates the runtime environment for each request. for a request, the last configuration in the Route configuration file is only one configuration. for this configuration, however, parsing the entire huge Route configuration file is a huge burden.

?? ? ? The REST style is a good way to solve this problem. The REST style architecture is adopted. the URL format of resources is relatively determined, and the operation method is determined by the HTTP method. Therefore, the Route configuration file will be quickly reduced.



?? ? ?? The REST software architecture is an abstract concept. it is an ultra-media distributed system designed to implement the Internet.Action Guide. This idea can be achieved using any technology. HTTP is the most famous software architecture. Generally, we also write REST as REST/HTTP. In practice, we often regard REST as an HTTP-based REST software architecture, or take REST and HTTP as an equivalent concept.

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.