tp5 路由学习
use think\Route;
// Route::rule('路由表达式','路由地址','请求类型','路由参数(数组)','变量规则(数组)');
// 路由地址:模块/控制器/操作 组成
// 请求类型: GET、POST、PUT、DELETE *(*代表前面的所有请求类型)
Route::rule('hello','sample/Test/hello','Get',['https'=>false]);
Route::rule('hello','sample/Test/hello','Get|POST',['https'=>false]);
Route::get('hello','sample/Test/hello');
Route::post('hello','sample/Test/hello');
Route::any('hello','sample/Test/hello'); //代表*
=================================================================
路由带参数访问路径:
Route::rule('hello/:id','sample/Test/hello');
http://localhost/tp/public/index.php/hello/123?name=zhangsan&age=18
代码访问方式:
第一种:
public function hello($id,$name,$age)
{
echo $id.$name.$age;
echo "sample Test hello....";
}
第二种:
use think\Request;
$id = Request::instance()->param('id');
$name = Request::instance()->param('name');
$age = Request::instance()->param('age');
或者所有的参数,保存到一个数组:
$all = Request::instance()->param();
获取问号后面的所有参数,保存到一个数组:$all = Request::instance()->get();
获取路径里面的参数,也就是问号前面的参数,保存到一个数组:$all = Request::instance()->route();
获取post body中传递的参数:$all = Request::instance()->post();
都可以通过带参数,获取指定的参数值。
第三种:助手函数的方法 param get post
$all =input(‘param.’); 获取所有参数
$all =input(‘post.age’); 获取单个参数
$all =input(‘get.name’); 获取单个参数
*** 赖注入的方法public function hello(Request $req)
浙公网安备 33010602011771号