美文网首页
ThinkPHP基础-----第五章(路由相关)第二部分

ThinkPHP基础-----第五章(路由相关)第二部分

作者: 大菜鸟呀 | 来源:发表于2018-08-23 20:00 被阅读6次

开启路由设置:
设置路由:

    1、动态单个设置:
        thinkphp->library->think->Route.php  
        1、开启路由:application/config.php;
                    // 是否开启路由
                    'url_route_on'      => true,
                    // 是否强制使用路由
                    'url_route_must'    => true,
                    关闭强制路由和是否使用路由

    2、设置路由:application->route.php;
        //引入系统类
        use think\Route;

        //定义路由规则,设置路由的页面就不能用PATH_INFO访问
        //Route::rule('路由表达式','路由地址','请求类型','路由参数(数组)','变量规则(数组)')
        Route::rule('/','index/index/index');
        Route::rule('test','index/index/test');

    3、路由形式:
        a、静态地址路由
            //注册路由test 访问到Index模块index控制器testfangfa 
            Route::rule('test','index/index/test');

        b、给路由带参数
        -----------------
        1、单个参数:(路由设置一个参数,访问必须带一个参数)
            route.php:
                //带参数路由
                Route::rule('course/:id','Index/index/course');
            index.php(application/index/Index.php):
                //带参数的路由
                public function course()
                {
                    return input('id');
                }
            访问:www.tp.com/course/10   (10为参数)  
        ------------------------
        2、多个参数:(路由设置几个参数,访问必须带几个参数)
            route.php:
                //带多个参数路由
                Route::rule('time/:y/:m/:d','Index/index/time');
            index.php(application/index/Index.php):
                //带多个参数的路由
                public function time()
                {
                    return input('y').'-'.input('m').'-'.input('d');
                }
            访问:www.tp.com/course/2018/08/23   (2018/08/23为参数)
        -----------------------------
        3、可选参数:(可选参数放后面)
            route.php:
                //可选参数路由
                Route::rule('times/:y/:m/[:d]','Index/index/times');
            index.php(application/index/Index.php):
                //带多个参数的路由
                public function times()
                {
                    return input('y').'-'.input('m').'-'.input('d');
                }
            访问:www.tp.com/course/2018/08  (2018/08/23为参数) 
            访问:www.tp.com/course/2018/08/23  (2018/08/23为参数)
        ---------------------------------
        4、全  动态参数
            route.php:
                //可选参数路由
                Route::rule(':a/:b','Index/index/dt');
            index.php(application/index/Index.php):
                //带多个参数的路由
                public function dt()
                {
                    return input('a').'-'.input('b');
                }
        ---------------------------------
        5、全 匹配路由
              route.php:
                //全匹配路由
                Route::rule('texts$','Index/index/aaa');
            index.php(application/index/Index.php):
                //全匹配路由
                    public function aaa()
                   {
                       return '这是全匹配路由';
                    }
            全匹配路由:多任何参数都不能匹配
        -------------------------------- 
        6、路由额外的参数
            route.php:
              //带额外参数的路由
                Route::rule('bbb','Index/index/bbb?id=10&name=zhangsan');
            index.php(application/index/Index.php):
                    //带额外的参数
                    public function bbb()
                    {
                       // dump(input());
                        echo input('id');
                        echo input('name');

                    }  

    4、设置请求类型:
        1、GET、POST、PUT、DELETE

        2、设置请求方式:默认支持所有类型
            Route::rule('course/:id','Index/index/course','get');
            Route::get('course/:id','Index/index/course');




2、动态批量设置:
    Route::rule([
        '路由参数1'=>'路由地址和参数',
        '路由参数2'=>['路由地址和参数','匹配参数']

    ],'','请求类型','批量参数(数组)','变量规则')

3、使用配置文件批量设置:
    return[
        '路由参数1'=>'路由地址和参数',
        '路由参数2'=>['路由地址和参数','匹配参数']
    ]

相关文章

网友评论

      本文标题:ThinkPHP基础-----第五章(路由相关)第二部分

      本文链接:https://www.haomeiwen.com/subject/amguiftx.html