tp5--路由的使用方法(深入)


懶得寫注釋,直接上代碼

配置文件Route:

 1 <?php
 2 use think\Route;
 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 app\index\controller;
 3 use app\index\controller\Base;
 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM