TP5中注册路由后,在后台无法获取传递变量之解决

因为项目需要,隐藏url中的敏感路径,就使用到了Tp5中的项目下route.php,但是按着手册修改route.php后在后台控制器中获取不到参数值:

1、前提在config.php中添加:

'url_route_on'  =>  true,  //开启路由

2、然后修改项目下 例如application/route.php
Route::bind('index'); //绑定模块
//批量注册变量
Route::pattern([
  'name' => '\w+',
  'id' => '\d+',
  'p' => '\d+'
]);
//路由规则
Route::get([
    'jiameng/:name'=>['index/listShow',[],['name'=>'\w+']],
]);

3、控制器中方法接受数据:例如URL为:http://www.jm.com/jiameng/feiyong   正常就收到的参数为  name=>feiyong

public function listShow()
{
   print_r($_GET); //获取不到数据
   dump(request()); //数据如下
}

dump(request()) 数据:

object(think\Request)#2 (32) {
  ["method":protected] => string(3) "GET"
  ["domain":protected] => NULL
  ["url":protected] => string(16) "/jiameng/feiyong"
  ["baseUrl":protected] => NULL
  ["baseFile":protected] => string(10) "/index.php"
  ["root":protected] => string(0) ""
  ["pathinfo":protected] => string(15) "jiameng/feiyong"
  ["path":protected] => string(15) "jiameng/feiyong"
  ["routeInfo":protected] => array(4) {
    ["rule"] => array(2) {
      [0] => string(7) "jiameng"
      [1] => string(5) ":name"
    }
    ["route"] => string(14) "index/listShow"
    ["option"] => array(1) {
      ["complete_match"] => bool(true)
    }
    ["var"] => array(1) {
      ["name"] => string(7) "feiyong"
    }
  }
  ["dispatch":protected] => array(3) {
    ["type"] => string(6) "module"
    ["module"] => array(3) {
      [0] => NULL
      [1] => string(5) "index"
      [2] => string(8) "listShow"
    }
    ["convert"] => bool(false)
  }
  ["module":protected] => string(5) "index"
  ["controller":protected] => string(5) "Index"
  ["action":protected] => string(8) "listShow"
  ["langset":protected] => string(5) "zh-cn"
  ["param":protected] => array(2) {
    ["/jiameng/feiyong"] => string(0) ""
    ["name"] => string(7) "feiyong"
  }
  ["get":protected] => array(1) {
    ["/jiameng/feiyong"] => string(0) ""
  }
  ["post":protected] => array(0) {
  }
  ["request":protected] => array(0) {
  }
  ["route":protected] => array(1) {
    ["name"] => string(7) "feiyong"
  }
  ["put":protected] => NULL
  ["session":protected] => array(0) {
  }
  ["file":protected] => array(0) {
  }
  ["cookie":protected] => array(0) {
  }
  ["server":protected] => array(0) {
  }
  ["header":protected] => array(9) {
    ["accept-language"] => string(14) "zh-CN,zh;q=0.9"
    ["accept-encoding"] => string(13) "gzip, deflate"
    ["referer"] => string(27) "http://www.jm.com/index.php"
    ["accept"] => string(85) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
    ["user-agent"] => string(114) "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
    ["upgrade-insecure-requests"] => string(1) "1"
    ["cache-control"] => string(9) "max-age=0"
    ["connection"] => string(5) "close"
    ["host"] => string(10) "www.jm.com"
  }
  ["mimeType":protected] => array(12) {
    ["xml"] => string(42) "application/xml,text/xml,application/x-xml"
    ["json"] => string(62) "application/json,text/x-json,application/jsonrequest,text/json"
    ["js"] => string(63) "text/javascript,application/javascript,application/x-javascript"
    ["css"] => string(8) "text/css"
    ["rss"] => string(19) "application/rss+xml"
    ["yaml"] => string(28) "application/x-yaml,text/yaml"
    ["atom"] => string(20) "application/atom+xml"
    ["pdf"] => string(15) "application/pdf"
    ["text"] => string(10) "text/plain"
    ["image"] => string(71) "image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/webp,image/*"
    ["csv"] => string(8) "text/csv"
    ["html"] => string(35) "text/html,application/xhtml+xml,*/*"
  }
  ["content":protected] => NULL
  ["filter":protected] => string(0) ""
  ["bind":protected] => array(0) {
  }
  ["input":protected] => string(0) ""
  ["cache":protected] => NULL
  ["isCheckCache":protected] => NULL
}

说明name在request中的route中是存在的。

后来直接在控制中打印:

var_dump(request()->route('name'));  //输出 feiyong

4、查看源码后发现可以直接在控制器下的方法中传递形参可以减少代码量 优化为:

public function listShow($name)
{
        echo $name; //输出为  feiyong   
}

至此就可以愉快的玩耍route了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值