thinkphp5.0 路由学习(二)

今天我纠结了一下,我是全部写完还是,把我感觉重要的常用的写下来呢,想了想,先把自己常用的写下来吧

动态单个路由的注册

所有的路由都是在application里面的route.php文件里面编写

  1. 引入系统类
  2. 书写基本路由规则
use think\Route;
Route::rule('user','index/index/user');

如果想路由带上参数呢

  • 带上一个参数
use think\Route;
Route::rule('user/:id','index/index/user');
//输入相当于
http://www.tp.com/user/1
http://www.tp.com/index.php/index/index/user/id/1

如果想带多个参数呢

use think\Route;
Route::rule('user/:year/:month','index/index/user');
//输入相当于
http://www.tp.com/user/2018/5
http://www.tp.com/index.php/index/index/user/year/2018/month/5

如果想带的是可选参数

use think\Route;
Route::rule('user/:year/[:month]','index/index/user');

完全匹配路由

use think\Route;
//完全匹配的意思就是,后面不能带参数,带参数就错误了
Route::rule('user$','index/index/user');

我想批量注册路由

//Route::rule([
//'路由规则1'=>'路由地址和参数',
//'路由规则2'=>['路由地址和参数','匹配参数(数组)','变量规则(数组)']
//...
//],'','请求类型','匹配参数(数组)','变量规则');
use think\Route;
Route::rule([
'new/:id'  =>  'News/read',
'user/:id'  =>  'News/user',
]);

除了用系统类,还有一种方法,直接return 路由规则,默认就是

return [
    'u/:year/:month'=>'index/user/index'
]

其实有几点我没有写,
路由的变量规则其实是用正则来控制,路由的输入的参数
路由参数其实是,路由的额外参数,比如,后缀等。
资源后台,其实是一次自动生成7个路由
快捷路由,其实是在方法名称钱自动添加get

猜你喜欢

转载自blog.csdn.net/weixin_42249565/article/details/80454217
今日推荐