在 Laravel 中,路由定義位于項目的 routes
目錄下的 web.php
(用于 web 請求)和 api.php
(用于 API 請求)文件中。要定義一個新的路由,你需要使用 Route
類的靜態方法,如 get
、post
、put
、delete
等。
以下是一些基本的路由定義示例:
Route::get('/example', function () {
return 'Hello, this is an example route!';
});
Route::get('/user/{id}', function ($id) {
return 'User ID: ' . $id;
});
Route::post('/user', function () {
// 處理 POST 請求數據
});
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', function () {
return 'Welcome to the dashboard!';
});
});
Route::get('/user/profile', function () {
return 'User Profile';
})->name('user.profile');
Route::get('/user/{id}', function ($id) {
return 'User ID: ' . $id;
})->where('id', '[0-9]+');
這些示例僅涉及 Laravel 路由的基本功能。Laravel 提供了許多高級功能,如路由模型綁定、子路由、路由參數類型轉換等。你可以查閱 Laravel 文檔以獲取更多關于路由的信息:https://laravel.com/docs/routing