zoukankan      html  css  js  c++  java
  • tp5--路由的使用方法(深入)

    懒得写注释,直接上代码

    配置文件Route:

     1 <?php
     2 use thinkRoute;
     3 
     4 //tp5路由测试
     5 //动态注册
     6 //Route::rule('路由表达式','路由地址','请求类型','路由参数(数组)','变量规则(数组)');
     7 //     /[:abc]为可选参数,一般放在必选参数的最后
     8 //Route::rule('demo/:name/:age/[:abc]', 'index/Index/demo', 'GET|POST', ['domain'=>'shop.com','ext'=>'html'], ['name'=>'W+']);
     9 
    10 //路由闭包
    11 //Route::get('hello/:name',function($name){
    12 //    return 'Hello,'.$name;
    13 //});
    14 
    15 // '/'表示网站的public,这样设置则为首页路径
    16 //Route::get('/',function(){
    17 //    return '你的网站已关闭';
    18 //});
    19 
    20 //跳转网页
    21 //Route::get('demo1', 'http://www.baidu.com');
    22 
    23 
    24 //动态分组
    25 //Route::group('demo',[
    26 //    ':num' =>  ['index/Index/demo2',  ['method'=>'get|post' , 'ext'=>'ps'] , ['num' => 'd{2,4}'] ],
    27 //    ':str' =>  ['index/Index/demo3',  ['method'=>'get|post' , 'ext'=>'ps'] , ['str' => '[a-zA-Z]'] ],
    28 //    ':bool' => ['index/Index/demo4',  ['method'=>'get|post' , 'ext'=>'ps'] , ['bool'=> '0|1'] ],
    29 //]);
    30 
    31 
    32 //动态闭包分组
    33 //Route::group('demo', function(){
    34 //    Route::any(':num','index/Index/demo2',['method'=>'get|post' , 'ext'=>'ps'] ,  ['num' => 'd{2,4}'] );
    35 //    Route::any(':str','index/Index/demo3',['method'=>'get|post' , 'ext'=>'ps'] ,  ['str' => '[a-zA-Z]'] );
    36 //    Route::any(':bool','index/Index/demo4',['method'=>'get|post' , 'ext'=>'ps'] , ['bool' => '0|1'] );
    37 //});
    38 
    39 //将公共的放在一起
    40 //头重脚轻
    41 //Route::group(['name' => 'demo','method'=>'get|post','ext'=>'ps','prefix'=>'index/Index/'], function(){
    42 //    Route::any(':num','demo2',[] ,  ['num' => 'd{2,4}'] );
    43 //    Route::any(':str','demo3',[] ,  ['str' => '[a-zA-Z]'] );
    44 //    Route::any(':bool','demo4',[ 'ext'=>'ps'] , ['bool' => '0|1'] );
    45 //});
    46 
    47 //->改
    48 //Route::group('demo', function(){
    49 //    Route::any(':num','demo2');
    50 //    Route::any(':str','demo3');
    51 //    Route::any(':bool','demo4' );},
    52 //    ['method'=>'get|post','ext'=>'ps','prefix'=>'index/Index/'],
    53 //    ['num' => 'd{2,4}' , 'str' => '[a-zA-Z]' , 'bool' => '0|1' ]);
    54 
    55 
    56 //动态的路由别名定义
    57 //注意!路由别名不支持变量类型和路由条件判断,单纯只是为了缩短URL地址,并且在定义的时候需要注意避免和路由规则产生混淆。
    58 //Route::alias('index','index/Index',['ext'=>'html']);
    59 
    60 //动态黑白名单(allow白名单 , except黑名单)
    61 Route::alias('index','index/Index',['ext'=>'html','allow'=> 'demo2']);
    62 
    63 //批量注册
    64 return [
    65     //统一变量名称一样的变量规则,(如果个别同时定义,则以个别的为准)
    66 //    '__pattern__' => [
    67 //              'name' => 'w+'
    68 //    ],
    69 
    70 //     "demo/:name/:age/[:abc]" => ['index/Index/demo' , ['method' => 'GET|POST' , 'ext' => 'html'] , ['name'=> 'W+']],
    71 
    72     //路由分组
    73     //根据专参不同,来决定进入哪个控制器
    74 //    'demo/:num' =>  ['index/Index/demo2',  ['method'=>'get|post' , 'ext'=>'ps'] , ['num' => 'd{2,4}'] ],
    75 //    'demo/:str' =>  ['index/Index/demo3',  ['method'=>'get|post' , 'ext'=>'ps'] , ['str' => '[a-zA-Z]'] ],
    76 //    'demo/:bool' => ['index/Index/demo4',  ['method'=>'get|post' , 'ext'=>'ps'] , ['bool'=> '0|1'] ],
    77 
    78     //分组
    79 //    '[demo]' =>[
    80 //            ':num' =>  ['index/Index/demo2',  ['method'=>'get|post' , 'ext'=>'ps'] , ['num' => 'd{2,4}'] ],
    81 //            ':str' =>  ['index/Index/demo3',  ['method'=>'get|post' , 'ext'=>'ps'] , ['str' => '[a-zA-Z]'] ],
    82 //            ':bool' => ['index/Index/demo4',  ['method'=>'get|post' , 'ext'=>'ps'] , ['bool'=> '0|1'] ],
    83 //    ],
    84 
    85 //批量定义路由别名
    86 //    '__alias__' =>[
    87 //        'index' => ['index/Index',['ext'=>'html']],
    88 //    ]
    89 
    90 //黑白名称
    91 //    '__alias__' =>[
    92 //        'index' => ['index/Index',[
    93 //            'ext'=>'html',
    94 //            'allow' => 'demo2',
    95 //        ]],
    96 //    ]
    97 ];

    控制器:

     1 <?php
     2 namespace appindexcontroller;
     3 use appindexcontrollerBase;
     4 
     5 class Index extends Base
     6 {
     7     public function index()
     8     {
     9         return $this -> fetch();
    10     }
    11 
    12 
    13     public function demo($name,$age,$abc='')
    14     {
    15         echo "ThinkPHP5 路由使用方法说明!";
    16         echo "<br/>";
    17         echo "我的名字是" . $name;
    18         echo "<br/>";
    19         echo "今年我已经" . $age ."岁了";
    20         echo "<br/>";
    21         echo "我是可选变量:". $abc;
    22         echo "<br/>";
    23         echo "其中,我是没有路由化的参数:" . input('id');
    24     }
    25 
    26     public function demo2($num)
    27     {
    28         echo '我是路由2';
    29         echo "<br/>";
    30         echo "我只能是数字:" .$num;
    31     }
    32 
    33     public function demo3($str)
    34     {
    35         echo '我是路由3';
    36         echo "<br/>";
    37         echo "我只能是字母:" .$str;
    38     }
    39 
    40     public function demo4($bool)
    41     {
    42         echo '我是路由4';
    43         echo "<br/>";
    44         echo "我只能是布尔值:" .$bool;
    45     }
    46 
    47 }
  • 相关阅读:
    字符流与字节流的区别
    向文件尾部追加内容
    Hashmap实现原理及扩容机制详解
    HashMap的put和get方法原理
    关于数字化工厂&智能工厂建设 IT 经验总结
    @所有人,网易数字+大会报名通道正式开启!
    WinForm程序打包1之快速入门
    解决安装.NET Framework不受信任的根证书
    Cannot resolve com.sun:tools:1.8.0 错误解决
    IDEA 2020报“java:程序包XXXX不存在”或“java:找不到符号”
  • 原文地址:https://www.cnblogs.com/laijinquan/p/10111668.html
Copyright © 2011-2022 走看看