To define a basic route in Laravel, you need to follow these steps:
Open the routes/web.php
file in your Laravel project.
To define a route for a GET request, use the Route::get()
method, and specify the URL path and a closure that handles the logic for that route. For example, to define a route for the /home
URL, add the following code:
Route::get('/home', function () {
return view('home');
});
In this example, when a user visits the /home
URL, Laravel will execute the closure and return the home
view.
To define a route for a POST request, use the Route::post()
method instead. For example, to define a route for submitting a form to the /submit
URL, use the following code:
Route::post('/submit', function () {
// Handle form submission logic here
});
In this example, Laravel will execute the closure when a POST request is made to the /submit
URL.
Save the routes/web.php
file.
Note: These examples demonstrate defining routes using closures, but you can also use controller methods to handle route logic for better organization and separation of concerns.