3.2Laravel控制器(Controller)之路由(Routing)

资源路由

Route::resource('photos', PhotoController::class);
VerbURIActionRoute Name
GET/photosindexphotos.index
GET/photos/createcreatephotos.create
POST/photosstorephotos.store
GET/photos/{photo}showphotos.show
GET/photos/{photo}/editeditphotos.edit
PUT/PATCH/photos/{photo}updatephotos.update
DELETE/photos/{photo}destroyphotos.destroy

局部资源路由

use App\Http\Controllers\PhotoController;

Route::resource('photos', PhotoController::class)->only([
    'index', 'show'
]);

Route::resource('photos', PhotoController::class)->except([
    'create', 'store', 'update', 'destroy'
]);

嵌套资源路由

//会生成 /photos/{photo}/comments/{comment}
use App\Http\Controllers\PhotoCommentController;

Route::resource('photos.comments', PhotoCommentController::class);

字符串路由

Route::get('/greeting', function () {
    return 'Hello World';
});

控制器路由

Route::get('/user', [UserController::class, 'index']);

带参数的路由

Route::get('/user/{id}', function ($id) {
    return 'User '.$id;
});
Route::get('/posts/{post}/comments/{comment}', function ($postId, $commentId) {
    //
});

重导向路由

Route::redirect('/here', '/there');

视图路由

Route::view('/welcome', 'welcome', ['name' => 'Taylor']);

命名路由

Route::get(
    '/user/profile',
    [UserProfileController::class, 'show']
)->name('profile');
route('profile');

模型路由

Route::get('/users/{user}', function (User $user) {
    return $user->email;
});
route:list

原创文章,作者:huoxiaoqiang,如若转载,请注明出处:https://www.huoxiaoqiang.com/php/laravel/5402.html

发表评论

登录后才能评论