路由传参
1、路由设置里面加入参数(路由路径加入:参数名)
2、URL后面用问号传入参 (?参数名=参数值)
http://zz.cn/hello/123/?name=gaoxigang
Route::get('hello/:id','sample/test/hello');
post方法传值
Route::post('hello/:id','sample/test/hello');
操作方法获取参数
1、函数里面定义参数
public function hello($id,$name){ echo $id; echo '|'; echo $name; }
2、request对象方法(不区分get、post类型)
use think\Request
①分别获取
$id = Request::instance()->param('id'); $name = Request::instance()->param('name'); $age = Request::instance()->param('age');
②获取所有的(返回数组)
$all = Request::instance()->param();
③获取具体的
get方法 获取?后面的参数
$id = Request::instance()->get('name');
route方法 获取路由里面设置的参数
$id = Request::instance()->route('id');
post方法 获取post提交的参数
$id = Request::instance()->post('age');
3、inpute(‘param’)方法获取 (返回数组)
$id = input('id');
4、依赖注入 Request
use think\Request; class Test { public function hello(Request $request){ $all = $request->param(); var_dump($all); } }