Learn the knowledge thinkphp must understand (ii)--thinkphp routing

Source: Internet
Author: User
<span id="Label3"></p><p><p>thinkphp supports the URL routing feature, you need to set the router_on parameter to true to enable Routing. When routing is turned on, routing detection is performed automatically, and routing resolution and redirection is performed if a route name that matches the current URL is found in the route Definition. The routing feature needs to define a route definition file, located under the Project's configuration directory, with the file name routes.php, which defines the format:</p></p> <p><p>PHP code</p></p> <p><p>Return Array (</p></p> <p><p>First Way General routing</p></p> <p><p>' RouteName ' =>array (' module name ', ' operation name ', ' parameter definition ', ' extra parameters '),</p></p> <p><p>Second way Generic Routing</p></p> <p><p>'<span class="__cf_email__" data-cfemail="3e6c514b4a5b705f535b7e">[email protected]</span>' =>array (</p></p> <p><p>Array (' route matching regular ', ' module name ', ' operation name ', ' parameter definition ', ' extra parameters '),</p></p> <p><p>),</p></p> <p><p>)</p></p> <p><p>When the system performs dispatch parsing, it will determine whether the current URL has a defined route name, and if so, URL parsing will follow the defined routing Rules. For example, We enabled the routing feature and defined the following<br>A Routing Rule:</p></p> <p><p>PHP code</p></p> <p><p>' Blog ' =>array (' blog ', ' index ', ' year,month,day ', ' userid=1&status=1′ ')</p></p> <p><p>Pan-routing Support</p></p> <p><p>The new version introduces generic routing support, which provides support for multiple rules for the same route name, making the URL more flexible, for example, where we need to route multiple rules for a blog's routing name:</p></p> <p><p>PHP code</p></p> <p><p>'<span class="__cf_email__" data-cfemail="e7a58b8880a7">[email protected]</span>' =>array (</p></p> <p><p>Array ('/^\/(\d+) (\/p\/\d)? $/', ' Blog ', ' read ', ' ID '),</p></p> <p><p>Array ('/^\/(\d+) \ (\d+)/', ' Blog ', ' archive ', ' Year,month '),</p></p> <p><p>),</p></p> <p><p>The first routing rule represents a read operation that resolves URLs such as blog/123 to the Blog module</p></p> <p><p>The second routing rule represents a archive operation that resolves URLs such as blog/2007/08 to the Blog module</p></p> <p><p>The definition of a generic route is more difficult than the definition of a regular route, and the use of other parameters is consistent with regular routing.</p></p> <p><p>As an example of a simple route, if we have a city module, and we want to be able to access the operation of a particular town through a URL address like the following:</p></p> <p><p>http://serverName/index.php/City/shanghai/</p></p> <p><p>Shanghai This method of operation does not exist, we have defined a city method for the relevant urban operation, as Follows:</p></p> <p><p>PHP code</p></p> <p><p>Class Cityaction extends action{</p></p> <p><p>Public Function City () {</p></p> <p><p>Read City Name</p></p> <p><p>$cityName = $_get[' name '];</p></p> <p><p>echo (' current city: '. $cityName);</p></p> <p><p>}<br>}</p></p> <p><p>Next we define the routing file, implementing a similar</p></p> <p><p>http://serverName/index.php/City/shanghai/</p></p> <p><p>Such parsing, the routing file name is</p></p> <p><p>PHP code</p></p> <p><p>Return Array (</p></p> <p><p>' City ' =>array ("city", ' city ', ' name ');</p></p> <p><p>);</p></p> <p><p>In this way, all the city modules in the URL will be routed to the city operation of the city module, and the second parameter will be parsed to $_get[' name '.</p></p> <p><p>next, we can enter it in the browser</p></p> <p><p>http://serverName/index.php/City/beijing/</p></p> <p><p>http://serverName/index.php/City/shanghai/</p></p> <p><p>http://serverName/index.php/City/shenzhen/</p></p> <p><p>You will see that the result is output in turn:</p></p> <p><p>Current City: Beijing</p></p> <p><p>Current City: Shanghai</p></p> <p><p>Current City: Shenzhen</p></p> <p><p></p></p> <p><p></p></p></span>

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.