Routing in Laravel is the process of defining how your application responds to HTTP requests. Routes map URLs to corresponding actions, closures, or controllers, allowing for clean and organized request handling.
For example, a route may define that when a user visits /about
,
they are shown the "About Us" page.
Handles requests to retrieve data from the server.
Route::get('/home', function () { return view('home'); });
Used to submit data to the server.
Route::post('/submit', function (Request $request) { return $request->all(); });
Updates an existing resource.
Route::put('/update/{id}', function ($id) { return "Update resource with ID $id"; });
Partially updates an existing resource.
Route::patch('/modify/{id}', function ($id) { return "Partially modify resource with ID $id"; });
Deletes an existing resource.
Route::delete('/delete/{id}', function ($id) { return "Delete resource with ID $id"; });
Responds to a client with the HTTP methods allowed for a specific resource.
Route::options('/options', function () { return response()->json(['GET', 'POST', 'DELETE']); });
Matches multiple HTTP methods for a single route.
Route::match(['get', 'post'], '/match', function () { return "This route works for GET and POST"; });
Responds to requests of all HTTP methods.
Route::any('/any', function () { return "Works for any HTTP method"; });
Provides a name for a route to generate URLs dynamically.
Route::get('/profile', 'ProfileController@show')->name('profile'); $url = route('profile');
Automatically handles CRUD operations for a resource.
Route::resource('posts', 'PostController');
Handles unmatched routes to provide a custom 404 page or message.
Route::fallback(function () { return response()->json(['message' => 'Page not found'], 404); });