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