老莫的笔记本  
  
查看: 966|回复: 3

TP5 路由

[复制链接]

662

主题

878

帖子

5145

积分

超级版主

Rank: 8Rank: 8

积分
5145
发表于 2018-8-1 00:39:14 | 显示全部楼层 |阅读模式
本帖最后由 周大胖子 于 2018-8-3 23:25 编辑

路由的作用:
1.根据事先定义的路由规则,检验URL请求,请求执行或者拒绝;
2.路由规则可自定义,隐藏原URL地址,【注意 定义路由规则后,无法直接访问原路由】;

路由规则写在哪?
1.路由规则写在与应用配置同级的router.php文件中;【我这写哪呢? 我这由于自定义了应用配置,所以我写在config/config 文件】;
2.路由规则主要使用类Route::rule() 方法注册;

注释: 1. 去thinkphp/convention.php 中查看是否开启了路由配置;【 url_route_on  是true 表示开启了路由】
2. 新建一个router.php 路由配置文件。【位置 就是在应用配置的同级目录中  config/router.php】
3. 使用 router 配置路由;   // 配置成功后,之前的路由路径不可访问 。
  1. // 使用route 类    think 前不用加 反斜杠
  2. think\Route::rule('retDemo','articles/test/retDemo');
复制代码

回复

使用道具 举报

662

主题

878

帖子

5145

积分

超级版主

Rank: 8Rank: 8

积分
5145
 楼主| 发表于 2018-8-2 00:56:20 | 显示全部楼层
路由三大模式:
1.普通模式: 配置 'url_route_on' => false;
2.混合模式: 配置'url_route_on' => true ;   'url_route_must' => false;
3.强制模式: 配置'url_route_on' => true ;   'url_route_must' => true;
回复

使用道具 举报

662

主题

878

帖子

5145

积分

超级版主

Rank: 8Rank: 8

积分
5145
 楼主| 发表于 2018-8-2 01:36:45 | 显示全部楼层
路由的注册
1.动态注册:
ROute::rule('路由规则','路由地址','请求类型',[路由参数],[变量规则]);
  1. // 动态路由  没有变量lesson 就是静态 注意这个 lesson 就是 变量名称 不是特定的
  2. think\Route::rule('lmlet/:lesson','articles/test/lmlet','GET',['ext'=>'shtml'],['lesson'=>'\w{1,10}']);
  3. think\Route::rule('lmpet/:akkk','articles/test/lmpet','GET',['ext'=>'shtml'],['akkk'=>'\w{1,10}']);
复制代码

2.数组配置(配置文件的方式):
return[
    '路由规则' => ‘路由地址’,
    '路由规则' =>  ['路由地址',[路由参数],[变量规则]],
];
  1. // 上面全注释后可以用配置路由  和配置项差不多 用return 返回数据
  2. return [
  3.     'demo/:lesson' =>['index/index/demo',['method'=>'get','ext'=>'shtml'],['lesson'=>'w{1,10}']],
  4.     'lmret' => 'articles/test/lmret',
  5. ];
复制代码

贴一下 test 那几个函数作为参考
  1. <?php
  2. namespace app\articles\controller;
  3. use think\config ;
  4. class Test
  5. {
  6.     public function lmret()
  7.     {
  8.         return '周大胖子身高    }
  9.     public function lmlet($lesson)
  10.     {
  11.         return '周大胖子身高'.$lesson;
  12.     }
  13.     public function lmpet($akkk)
  14.     {
  15.         return 'PTTTTTTTTTTTTTTT'.$akkk;
  16.     }
  17.     public function retD()
  18.     {
  19.         dump(Config::get());
  20.     }
  21. }
复制代码

再贴一下动态路由的访问链接样式:http://localhost/public/index.php/lmpet/phpp.shtml  【phpp 就是传入的值,  .shtml 就是规定的url 后缀】





回复

使用道具 举报

662

主题

878

帖子

5145

积分

超级版主

Rank: 8Rank: 8

积分
5145
 楼主| 发表于 2018-8-3 23:28:48 | 显示全部楼层
路由规则
路由规则:参数 变量的传递等 不说了 上两段代码第一个PHP
  1. <?php
  2. // 使用route 类    think 前不用加 反斜杠
  3. // think\Route::rule('lmret','articles/test/lmret');
  4. // 动态路由  没有变量lesson 就是静态 注意这个 lesson 就是 变量名称 不是特定的
  5. think\Route::rule('lmlet/:lesson','articles/test/lmlet','GET',['ext'=>'shtml'],['lesson'=>'\w{1,10}']);
  6. think\Route::rule('lmpet/:akkk','articles/test/lmpet','GET',['ext'=>'shtml'],['akkk'=>'\w{1,10}']);
  7. // 多变量的配置
  8. think\Route::rule('lmtwo/:name/:sex','articles/test/lmtwo','GET',['ext'=>'html'],['name'=>'\w{1,10}','sex'=>'\w{1,10}']);
  9. // 可选多变量的配置  加个方括号 表示可选
  10. think\Route::rule('lmthree/:name/[:sex]','articles/test/lmthree','GET',['ext'=>'html'],['name'=>'\w{1,10}','sex'=>'\w{1,10}']);
复制代码

下面是  控制器中的类方法
  1. <?php
  2. namespace app\articles\controller;
  3. use think\config ;
  4. class Test
  5. {
  6.     public function lmret()
  7.     {
  8.         return '周大胖子身高    }
  9.     public function lmlet($lesson)
  10.     {
  11.         return '周大胖子身高'.$lesson;
  12.     }
  13.     public function lmpet($akkk)
  14.     {
  15.         return 'PTTTTTTTTTTTTTTT'.$akkk;
  16.     }
  17.     public function lmtwo($name,$sex)
  18.     {
  19.         return '我是'.$name.'我的性别是'.$sex ;
  20.     }
  21.     public function lmthree($name,$sex='男')
  22.     {
  23.         return '我是'.$name.'我的性别是'.$sex ;
  24.     }
  25.     public function retD()
  26.     {
  27.         dump(Config::get());
  28.     }
  29. }
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表