W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
當然,您可以獲取請求路由的 URI 區(qū)段。
Route::get('user/{id}', function($id)
{
return 'User '.$id;
});
Route::get('user/{name?}', function($name = null)
{
return $name;
})
Route::get('user/{name?}', function($name = 'John')
{
return $name;
});
Route::get('user/{name}', function($name)
{
//
})
->where('name', '[A-Za-z]+');
Route::get('user/{id}', function($id)
{
//
})
->where('id', '[0-9]+');
Route::get('user/{id}/{name}', function($id, $name)
{
//
})
->where(['id' => '[0-9]+', 'name' => '[a-z]+'])
如果你想讓特定路由參數總是遵詢特定的正則表達式,可以使用 pattern 方法。在 RouteServiceProvider 的 boot 方法里定義模式:
$router->pattern('id', '[0-9]+');
定義模式之后,會作用在所有使用這個特定參數的路由上:
Route::get('user/{id}', function($id)
{
// 只有 {id} 是數字才被調用。
});
如果需要在路由外部取得其參數,使用 input 方法:
if ($route->input('id') == 1)
{
//
}
你也可以使用 Illuminate\Http\Request 實體取得路由參數。當前請求的實例可以通過 Request facade 取得,或透過類型提示 Illuminate\Http\Request 注入依賴:
use Illuminate\Http\Request;
Route::get('user/{id}', function(Request $request, $id)
{
if ($request->route('id'))
{
//
}
});
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: