A 三种模式
1 路由是只针对应用
2 如果某模块不用路由 譬如关闭后台路由
在入口文件里,加载引导文件之后加上 \think\App::route(false);
1 普通模式
完全使用默认的PATH_INFO方式URL:
如:http://www.tp5.com/admin.php/Index/admintest
- <?php
- // 是否开启路由
- 'url_route_on' => false ,
- // 是否强制使用路由
- 'url_route_must' => false,
2 混合模式
既可以使用普通模式 也可以使用路由模式
注意:混合模式是对不同方法混合访问存在的,如果对同一个方法进行路由注册后 此方法的path_info的访问将失效
- <?php
- // 是否开启路由
- 'url_route_on' => true ,
- // 是否强制使用路由
- 'url_route_must' => false,
3 强制模式
只能使用路由模式
- <?php
- // 是否开启路由
- 'url_route_on' => true ,
- // 是否强制使用路由
- 'url_route_must' => true,
B 路由设置
注意:当某模块\控制器\方法 注册后 原来的访问地址会自动失效。
通常在 application/route.php进行注册,格式是:
Route::rule('路由表达式','路由地址','请求类型','路由参数(数组)','变量规则(数组)');
1 动态单个设置
- <?php
- use think\Route;
- // 注册路由到index模块的index控制器的index操作
- // 以下两个是静态地址路由
- Route::rule('/','index/index/index');
- Route::rule('about','index/index/about');
- //动态[必带参数]
- Route::rule('course/:id/:name','index/index/course'); //http://www.tp5.com/course/99/liming
- //动态[可选参数]
- Route::rule('course/[:id]/[:name]','index/index/course');
- //全动态路由 不建议使用 [如果参数a 与某个控制器名称匹配则优先匹配]
- Route::rule(':a/:b','index/index/quandongtai');
- // 完全匹配路由
- Route::rule("strict$",'index/index/strict'); //必须 www.tp5.com/strict 否则带任何参数都无法匹配此路由
- // 设置请求方式(第三参数可以 get|post) 也可以 Route::get() Route::post();
- Route::rule('type','index/index/type','get');
1,1 动态快速注册
- <?php
- Route::get('hello','example/test/hello');
- Route::post('hello','example/test/hello');
- Route::delete('hello','example/test/hello');
- Route::put('hello','example/test/hello');
1.2传参
带额外参数 【额外参数指的是不在URL里面的参数,隐式传入需要的操作中,有时候能够起到一定的安全防护作用】
第一参数后带的参数必传
- <?php
- Route::rule('api/:id'=>'index/index/api?status=1&app_id=5');
[ ]包含起来后就表示该变量是路由匹配的可选变量。
- <?php
- Route::rule('api/[:id]'=>'index/index/api?status=1&app_id=5');
1.3 自定义层级目录路由
例如:新建了 application\api\controller\v1\Banner.php 并建立了getBanner 方法
- <?php
- Route::rule('banner/:id','api/v1.Banner/getBanner');
2 动态批量注册
- <?php
- use think\Route;
- Route::rule([
- '/'=>'index/index/index',
- 'about'=>'index/index/about',
- 'course/[:id]/[:name]'=>'index/index/course',
- 'strict$'=>'index/index/strict',
- 'type'=>'index/index/type',
- ],'');
3 使用配置文件批量注册
- <?php
- //使用配置文件批量注册
- return [
- '/'=>'index/index/index',
- 'about'=>'index/index/about',
- 'course/[:id]/[:name]'=>'index/index/course',
- 'strict$'=>'index/index/strict',
- 'type'=>'index/index/type',
- ];
4 变量规则
//注意:当设置规则时 第三参数 不能为空 *,get,post,put,delete,get|post ......
- <?php
- use think\Route;
- //Route::rule('路由表达式' ,'路由地址', '请求类型','路由参数(数组)','变量规则(数组)');
- Route::rule('course/[:id]/[:name]','index/index/course','*',[],['id'=>'\d+']);
- //Route::get('路由表达式' ,'路由地址','路由参数(数组)','变量规则(数组)');
- Route::get('course/[:id]/[:name]','index/index/course',[],['id'=>'\d+']);
4.1 路由分组
- <?php
- // 商品接口
- // Route::get('api/:version/product/:id','api/:version.product/getOne',[],['id'=>'\d+']);
- // Route::get('api/:version/product/recent','api/:version.Product/getRecent');
- // Route::get('api/:version/product/by_category/:id','api/:version.product/getAllInCategory'); //获取某一分类下的所有商品
- Route::group('api/:version/product',function(){
- Route::get('/:id','api/:version.product/getOne',[],['id'=>'\d+']);
- Route::get('/recent','api/:version.Product/getRecent');
- Route::get('/by_category/:id','api/:version.product/getAllInCategory');
- });
5 路由参数(第四参数)
参数 说明
method 请求类型检测,支持多个请求类型
ext URL 后缀检测,支持匹配多个后缀
deny_ext URL 禁止后缀检测,支持匹配多个后缀
https 检测是否https请求
domain 域名检测
before_behavior 前置行为(检测)
after_behavior 后置行为(执行)
cache 请求缓存(V5.0.1+)
- <?php
- use think\Route;
- // 注册路由到index模块的index控制器的index操作
- Route::rule('/','index/index/index');
- Route::rule('about','index/index/about');
- //动态[可选参数]
- Route::rule('course/[:id]/[:name]','index/index/course','*',['method'=>'get','ext'=>'html','https'=>'true','cache'=>60],['id'=>'\d{1,3}','name'=>'\w+']);
- // 完全匹配路由
- Route::rule("strict$",'index/index/strict');
- // 带额外参数 【额外参数指的是不在URL里面的参数,隐式传入需要的操作中,有时候能够起到一定的安全防护作用】
- Route::rule('api','index/index/api?status=1&app_id=5');
- // 设置请求方式(第三参数可以 get|post) 也可以 Route::get() Route::post();
- Route::rule('type','index/index/type','get|post');
- //批量注册
- Route::rule([
- '/' =>'index/index/index',
- 'about' =>'index/index/about',
- 'course/[:id]/[:name]'=>'index/index/course',
- 'strict$' =>'index/index/strict',
- 'type' =>'index/index/type',
- ],'');
- //使用配置文件批量注册
- return [
- '/'=>'index/index/index',
- 'about'=>'index/index/about',
- 'course/[:id]/[:name]'=>'index/index/course',
- 'strict$'=>'index/index/strict',
- 'type'=>'index/index/type',
- ];
5 资源路由
是一种可以自动生成7 个路由的方法
- <?php
- use think\Route;
- // 以下指向 index前台模块的 blog控制器
- Route::resource('blog','index/blog');
千万注意:使用资源路由配合生成控制器的使用方法时
form表单提交一定不要具体到方法,只需到控制器 <form action="/user" method="post">
- <?php
- <form action="/user" method="post">
- <div class="form-group">
- <label for="name">User</label>
- <input type="text" placeholder="请输入用户名" name="name" class="form-control" id="name">
- </div>
- <div class="form-group">
- <label for="pass">Pass</label>
- <input type="password" placeholder="请输入密码" name="pass" class="form-control" id="pass">
- </div>
- <div class="form-group">
- <input type="submit" value="提交" class="btn btn-success">
- <input type="reset" value="重置" class="btn btn-warning">
- </div>
- </form>
6 快捷路由
也是一种自动生成路由的方法
- <?php
- use think\Route;
- // 给User控制器设置快捷路由
- Route::controller('user','index/User');
- User控制器定义如下:
- namespace app\index\controller;
- class User {
- public function getInfo()
- {
- }
- public function getPhone()
- {
- }
- public function postInfo()
- {
- }
- public function putInfo()
- {
- }
- public function deleteInfo()
- {
- }
- }
- 我们可以通过下面的URL访问
- get http://localhost/user/info
- get http://localhost/user/phone
- post http://localhost/user/info
- put http://localhost/user/info
- delete http://localhost/user/info
获取参数
- <?php
- 第一种
- public function hello($id='',$age='',$b=''){
- dump($id);
- dump($age);
- dump($b);
- }
- 第二种 input() input('param.') input('post.') input('get.')
- public function hello()
- {
- $id = input('id');
- $age = input('age');
- $name = input('name');
- dump($id);
- dump($age);
- dump($name);
- }
- 如果是 input('param.') input('post.') input('get.')将获得一个对应的数组
- 第三种 param get post put delete
- public function hello()
- {
- $id = Request::instance()->param('id');
- $age = Request::instance()->param('age');
- $name = Request::instance()->param('name');
- dump($id);
- dump($age);
- dump($name);
- }
- 可以 $id = Request::instance()->route(); 来专门获得一个数组 url 定义的参数
- 如 访问 http://z.cn/hello/6/a/19/b/90
- Array ( [id] => 6 [a] => 19 [b] => 90 )
C url地址的生成
1 使用 \think\Url::build
2 使用 系统函数 url( );
3 生成的url 遵循 后缀 'url_html_suffix' => 'shtml'
注意:如果使用了 路由,那么生成的url地址也会根据路由简化
- <?php
- //使用系统类 或者 使用系统方法
- echo \think\Url::build('index/user/index'); // /user/index.html
- //第三参数是 后缀
- echo \think\Url::build('index/user/index','','hhttmmll'); // /user/index.hhttmmll
- echo url('index/user/index',['id'=>'9','name'=>'ligang']); // /user/index/id/9/name/ligang.html
- //生成域名
- echo \think\Url::build('index/user/index','','',true); // http://www.tp5.com/user/index
- //指定域名
- echo \think\Url::build('index/user/index','','html','blog'); //http://blog.tp5.com/user/index.html
- echo \think\Url::build('index/user/index@blog','','html'); //http://blog.tp5.com/user/
- echo \think\Url::build('index/user/index@blog.thinkphp.cn','','html'); //http://blog.thinkphp.cn/user/index.html
- //生成锚点
- echo \think\Url::build('index/user/index#anthor@blog','','html'); //http://blog.tp5.com/user/index.html#anthor
- //添加入口文件
- \think\Url::root('/index.php');
- echo \think\Url::build('index/user/index','','html'); // index.php/user/index.html