tp5学习基础知识总结 2 路由,获取参数和url

A   三种模式

1 路由是只针对应用

2 如果某模块不用路由 譬如关闭后台路由

   在入口文件里,加载引导文件之后加上  \think\App::route(false);

 

1 普通模式

完全使用默认的PATH_INFO方式URL:

如:http://www.tp5.com/admin.php/Index/admintest

 
  1. <?php
  2. // 是否开启路由
  3.  'url_route_on' => false ,
  4. // 是否强制使用路由
  5. 'url_route_must' => false,

 

2 混合模式

既可以使用普通模式 也可以使用路由模式

注意:混合模式是对不同方法混合访问存在的,如果对同一个方法进行路由注册后  此方法的path_info的访问将失效

 
  1. <?php
  2. // 是否开启路由
  3.  'url_route_on' => true ,
  4. // 是否强制使用路由
  5.  'url_route_must' => false,

 

3 强制模式

只能使用路由模式

 
  1. <?php
  2. // 是否开启路由
  3.  'url_route_on' => true ,
  4. // 是否强制使用路由
  5. 'url_route_must' => true,

 

B   路由设置

注意:当某模块\控制器\方法 注册后  原来的访问地址会自动失效。

通常在 application/route.php进行注册,格式是:

Route::rule('路由表达式','路由地址','请求类型','路由参数(数组)','变量规则(数组)');

1 动态单个设置

 

 
  1. <?php
  2. use think\Route;
  3. // 注册路由到index模块的index控制器的index操作
  4. // 以下两个是静态地址路由
  5. Route::rule('/','index/index/index');
  6. Route::rule('about','index/index/about');
  7.  
  8. //动态[必带参数]
  9. Route::rule('course/:id/:name','index/index/course');  //http://www.tp5.com/course/99/liming
  10. //动态[可选参数]
  11. Route::rule('course/[:id]/[:name]','index/index/course');
  12.  
  13. //全动态路由 不建议使用 [如果参数a 与某个控制器名称匹配则优先匹配]
  14. Route::rule(':a/:b','index/index/quandongtai');
  15.  
  16. // 完全匹配路由
  17. Route::rule("strict$",'index/index/strict');   //必须 www.tp5.com/strict  否则带任何参数都无法匹配此路由
  18.  
  19. // 设置请求方式(第三参数可以 get|post) 也可以 Route::get() Route::post();
  20. Route::rule('type','index/index/type','get');

1,1 动态快速注册

 
  1. <?php
  2. Route::get('hello','example/test/hello');
  3. Route::post('hello','example/test/hello');
  4. Route::delete('hello','example/test/hello');
  5. Route::put('hello','example/test/hello');

 1.2传参

 带额外参数 【额外参数指的是不在URL里面的参数,隐式传入需要的操作中,有时候能够起到一定的安全防护作用】

 第一参数后带的参数必传

 
  1. <?php
  2. Route::rule('api/:id'=>'index/index/api?status=1&app_id=5');

  [ ]包含起来后就表示该变量是路由匹配的可选变量。

 
  1. <?php 
  2. Route::rule('api/[:id]'=>'index/index/api?status=1&app_id=5');

1.3 自定义层级目录路由

例如:新建了 application\api\controller\v1\Banner.php  并建立了getBanner 方法

 
  1. <?php
  2. Route::rule('banner/:id','api/v1.Banner/getBanner');

2 动态批量注册

 

 
  1. <?php
  2. use think\Route;
  3. Route::rule([
  4.         '/'=>'index/index/index',
  5.         'about'=>'index/index/about',
  6.       'course/[:id]/[:name]'=>'index/index/course',
  7.       'strict$'=>'index/index/strict',
  8.       'type'=>'index/index/type',
  9.             ],'');

3 使用配置文件批量注册

 
  1. <?php
  2. //使用配置文件批量注册
  3. return [
  4. '/'=>'index/index/index',
  5.     'about'=>'index/index/about',
  6.     'course/[:id]/[:name]'=>'index/index/course',
  7.     'strict$'=>'index/index/strict',
  8.     'type'=>'index/index/type',
  9. ];

4 变量规则

//注意:当设置规则时 第三参数 不能为空 *,get,post,put,delete,get|post ......

 

 
  1. <?php
  2. use think\Route;
  3. //Route::rule('路由表达式'     ,'路由地址', '请求类型','路由参数(数组)','变量规则(数组)');
  4. Route::rule('course/[:id]/[:name]','index/index/course','*',[],['id'=>'\d+']);
  5. //Route::get('路由表达式' ,'路由地址','路由参数(数组)','变量规则(数组)');
  6. Route::get('course/[:id]/[:name]','index/index/course',[],['id'=>'\d+']);

 4.1 路由分组

 

 
  1. <?php
  2. // 商品接口
  3. // Route::get('api/:version/product/:id','api/:version.product/getOne',[],['id'=>'\d+']);
  4. // Route::get('api/:version/product/recent','api/:version.Product/getRecent');
  5. // Route::get('api/:version/product/by_category/:id','api/:version.product/getAllInCategory'); //获取某一分类下的所有商品
  6.  
  7. Route::group('api/:version/product',function(){
  8. Route::get('/:id','api/:version.product/getOne',[],['id'=>'\d+']);
  9. Route::get('/recent','api/:version.Product/getRecent');
  10. Route::get('/by_category/:id','api/:version.product/getAllInCategory');
  11. });

 

5 路由参数(第四参数)


参数                                   说明
method                          请求类型检测,支持多个请求类型
ext                                  URL 后缀检测,支持匹配多个后缀
deny_ext                        URL 禁止后缀检测,支持匹配多个后缀
https                              检测是否https请求
domain                          域名检测
before_behavior            前置行为(检测)
after_behavior              后置行为(执行)
cache                             请求缓存(V5.0.1+)

 
  1. <?php
  2. use think\Route;
  3. // 注册路由到index模块的index控制器的index操作
  4. Route::rule('/','index/index/index');
  5. Route::rule('about','index/index/about');
  6.  
  7. //动态[可选参数]
  8. Route::rule('course/[:id]/[:name]','index/index/course','*',['method'=>'get','ext'=>'html','https'=>'true','cache'=>60],['id'=>'\d{1,3}','name'=>'\w+']);
  9.  
  10. // 完全匹配路由
  11. Route::rule("strict$",'index/index/strict');
  12.  
  13. // 带额外参数 【额外参数指的是不在URL里面的参数,隐式传入需要的操作中,有时候能够起到一定的安全防护作用】
  14. Route::rule('api','index/index/api?status=1&app_id=5');
  15.  
  16. // 设置请求方式(第三参数可以 get|post) 也可以 Route::get() Route::post();
  17. Route::rule('type','index/index/type','get|post');
  18.  
  19. //批量注册
  20. Route::rule([
  21.         '/'                   =>'index/index/index',
  22.         'about'               =>'index/index/about',
  23.       'course/[:id]/[:name]'=>'index/index/course',
  24.       'strict$'             =>'index/index/strict',
  25.       'type'                =>'index/index/type',
  26.             ],'');
  27.             
  28. //使用配置文件批量注册
  29. return [
  30. '/'=>'index/index/index',
  31.     'about'=>'index/index/about',
  32.     'course/[:id]/[:name]'=>'index/index/course',
  33.     'strict$'=>'index/index/strict',
  34.     'type'=>'index/index/type',
  35. ];
  36.  

 

5 资源路由

    是一种可以自动生成7 个路由的方法

 
  1. <?php
  2. use think\Route;
  3. // 以下指向 index前台模块的 blog控制器
  4. Route::resource('blog','index/blog');

千万注意:使用资源路由配合生成控制器的使用方法时

form表单提交一定不要具体到方法,只需到控制器  <form  action="/user" method="post">

 
  1. <?php
  2. <form  action="/user" method="post">
  3. <div class="form-group">
  4. <label for="name">User</label>
  5. <input type="text" placeholder="请输入用户名" name="name" class="form-control" id="name">
  6. </div>
  7. <div class="form-group">
  8. <label for="pass">Pass</label>
  9. <input type="password" placeholder="请输入密码" name="pass" class="form-control" id="pass">
  10. </div>
  11.  
  12. <div class="form-group">
  13. <input type="submit" value="提交" class="btn btn-success">
  14. <input type="reset" value="重置" class="btn btn-warning">
  15.  
  16. </div>
  17. </form>

6 快捷路由

   也是一种自动生成路由的方法

 
  1. <?php
  2. use think\Route;
  3. // 给User控制器设置快捷路由
  4. Route::controller('user','index/User');
  5.  
  6.  
  7. User控制器定义如下:
  8.  
  9. namespace app\index\controller;
  10.  
  11. class User {
  12.     public function getInfo()
  13.     {
  14.     }
  15.     
  16.     public function getPhone()
  17.     {
  18.     }
  19.     
  20.     public function postInfo()
  21.     {
  22.     }
  23.     
  24.     public function putInfo()
  25.     {
  26.     }
  27.     
  28.     public function deleteInfo()
  29.     {
  30.     }
  31. }
  32. 我们可以通过下面的URL访问
  33.  
  34. get http://localhost/user/info
  35. get http://localhost/user/phone
  36. post http://localhost/user/info
  37. put http://localhost/user/info
  38. delete http://localhost/user/info

获取参数 

 
  1. <?php
  2. 第一种
  3.     public function hello($id='',$age='',$b=''){
  4.      dump($id);
  5.      dump($age);
  6.      dump($b);
  7.     }
  8. 第二种 input()  input('param.')  input('post.')  input('get.')
  9.     public function hello()
  10.     {
  11.       $id = input('id');
  12.      $age = input('age');
  13.      $name = input('name');
  14.      dump($id);
  15.      dump($age);
  16.      dump($name);
  17.     }
  18.     如果是 input('param.')  input('post.')  input('get.')将获得一个对应的数组
  19. 第三种  param get post put delete
  20.     public function hello()
  21.     {
  22.       $id = Request::instance()->param('id');
  23.       $age = Request::instance()->param('age');
  24.       $name = Request::instance()->param('name');
  25.       dump($id);
  26.      dump($age);
  27.      dump($name);
  28.     }
  29.     可以 $id = Request::instance()->route();  来专门获得一个数组  url 定义的参数
  30.     如  访问  http://z.cn/hello/6/a/19/b/90
  31.     Array ( [id] => 6 [a] => 19 [b] => 90 )

 

C  url地址的生成

1 使用 \think\Url::build 

2 使用 系统函数 url( );

3 生成的url 遵循 后缀 'url_html_suffix' => 'shtml'  

注意:如果使用了 路由,那么生成的url地址也会根据路由简化

 
  1. <?php
  2. //使用系统类  或者  使用系统方法
  3. echo \think\Url::build('index/user/index');                  //   /user/index.html
  4.  
  5. //第三参数是 后缀
  6. echo \think\Url::build('index/user/index','','hhttmmll');    //   /user/index.hhttmmll
  7.  
  8. echo url('index/user/index',['id'=>'9','name'=>'ligang']);   //   /user/index/id/9/name/ligang.html
  9. //生成域名
  10. echo \think\Url::build('index/user/index','','',true);       //   http://www.tp5.com/user/index
  11. //指定域名
  12. echo \think\Url::build('index/user/index','','html','blog'); //http://blog.tp5.com/user/index.html
  13. echo \think\Url::build('index/user/index@blog','','html');   //http://blog.tp5.com/user/
  14. echo \think\Url::build('index/user/index@blog.thinkphp.cn','','html'); //http://blog.thinkphp.cn/user/index.html
  15.  
  16. //生成锚点
  17. echo \think\Url::build('index/user/index#anthor@blog','','html'); //http://blog.tp5.com/user/index.html#anthor
  18.  
  19. //添加入口文件
  20. \think\Url::root('/index.php');
  21. echo \think\Url::build('index/user/index','','html');    //        index.php/user/index.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值