標籤:ram 擷取參數 總結 att pre res ret get 函數
我今天下午主要學習了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方式
thinkphp 參數傳遞方式(基礎)