我今天下午主要學習了thinkphp5.0的路由部分,我下面總結一下我主要學習到的知識點:
路由定義:
有兩種方式:
(1).動態注冊:
eg:
Route::rule('hello','index/index/hello','GET');
(2)配置式:
eg:
return [
'__pattern__' => [
'name' => '\w+',
],
'[hello]' => [
':id' => ['index/hello', ['method' => 'get'], ['id' => '\d+']],
':name' => ['index/hello', ['method' => 'post']],
],
];
請求類型:
類型 | 描述 |
---|---|
GET | GET請求 |
POST | POST請求 |
PUT | PUT請求 |
DELETE | DELETE請求 |
* | 任何請求類型 |
eg:
Route::get('new/:id','News/read'); // 定義GET請求路由規則 Route::post('new/:id','News/update'); // 定義POST請求路由規則 Route::put('new/:id','News/update'); // 定義PUT請求路由規則 Route::delete('new/:id','News/delete'); // 定義DELETE請求路由規則 Route::any('new/:id','News/read'); // 所有請求都支持的路由規則
獲取參數的方法 [三種 ]:
1).方法內變量的對應
public function hello($id,$name)
{
echo $id;
echo $name;
}
2).Request對象
Requeset::instance=>param();//獲取所有參數[ 結果類型數組],不分請求類型;
Requeset::instance=>param('name');//獲取單個參數[即:直接填寫變量名即可];
Requeset::instance=>get();//獲取?后面的參數;
Requeset::instance=>route();//獲取路由里面的參數;
Requeset::instance=>post();//獲取post請求參數
eg:
public function hello()
{
$res=Request::instance()->param();
var_dump($res);
}
依賴注入方式
public function hello(Request $request)
{
$res=$request->param();
var_dump($res);
}
3).使用input助手函數
input('param'); //獲取所有結果數組
input('param.name'); //獲取name
input('get.name'); //獲取post方式
input('get.name'); //獲取get方式