thinkphp3.2.3 to set up routes and optimize URLs

Source: Internet
Author: User

Requirements: When accessing this directory, http://xx.com/p-412313 is redirected to (for the time being) http://xx.com/Home/Blog/index/id/412313


Just looking good.

My application directory is application. Module is home

Step one: Know which file to do with the route
Routing processing in think/route.class.php

Dynamic routing processing
        $routes =   C (' url_route_rules ');
        Var_dump ($routes);

        if (!empty ($routes)) {
            //dump ($routes);
            Array (1) {
                //["/^p-(\d+) $/"] => string "blog/index?id=:1"/
            /}
            //echo "rount.class.php\n";
            foreach ($routes as $rule => $route) {
                if (is_numeric ($rule)) {
                    //support Array (' rule ', ' adddress ',...) to define routing
                    $rule   =   array_shift ($route);
                }
     ... More code, identify location only

Step two: Under the configuration file of the project module, set the configuration file as follows (because TP is a step-by-step configuration file, the custom configuration-> application configuration-> mode matching


-> Debug Configuration-> state configuration-> Module configuration-> Extended configuration-> dynamic configuration, which belongs to the module configuration

<?php return
Array ('


    module_allow_list ' => Array (' Home ', ' Admin ', ' Common '),
     ' Default_module '       =>    ' home ',  ///default module
     ' Default_controller    '  => ' Index ',//Default controller name
     ' Default_ Action '        =>  ' index ',///default action name
         //    Routing Rule
    ' url_router_on ' => TRUE,
    ' Url_route_ RULES ' => Array (
        '/^c-(\d+) $/' => ' index/content?id=:1 '
    ),
	 ' url_router_on ' =>true,
     ' Url_model '          => ' 1 ',

     ' url_route_rules ' =>array ('/^p-' \d+ ' $/' => ' home/blog/index?id=:1 '   
       ,/ It means to visit the x.com/p-34 will visit the x.com/blog/index/id/34

         )
;

Part III:
If you do not set the default module, there will be a failure to find p-34 this module.
Then you need to set up in index.php
Define (' Bind_module ', ' home ');

That's it.

Description: If there is no third step. It is normal to open by default.

But



So we do the third step, we can achieve the following effect:







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.