1.路由、中间件、csrf

2019-12-03  本文已影响0人  darren911

基本路由

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
Route::match(['get', 'post'], '/', function () {
    //
});
Route::any('/', function () {
    //
});
Route::redirect('/here', '/there');
Route::redirect('/here', '/there', 301); //默认302
Route::get('user/{id}', function ($id) {
    return 'User '.$id;
});
//多个参数
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
    //
});
Route::get('user/{name?}', function ($name = null) {
    return $name;
});
Route::get('user/{name?}', function ($name = 'John') {
    return $name;
});
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]+']);
Route::get('user/profile', 'UserProfileController@show')->name('profile');
// 生成 URL...
$url = route('profile');
// 生成重定向...
return redirect()->route('profile');
//如果有定义参数的
Route::get('user/{id}/profile', function ($id) {
    //
})->name('profile');
$url = route('profile', ['id' => 1]);
//如果你想判断当前请求是否指向了某个命名过的路由,你可以调用路由实例上的 named 方法
if ($request->route()->named('profile')) {
        //
    }
上一篇 下一篇

猜你喜欢

热点阅读