Getting Started with Laravel : Laravel Controllers Handling HTTP POST Requests for Form Submissions
Getting Started with Laravel: Handling HTTP POST Requests with Controllers
Laravel is a popular PHP framework that makes it easy to develop web applications with a clean and elegant syntax. One of the fundamental aspects of web applications is handling form submissions, especially when it comes to HTTP POST requests. In this blog post, we will explore how to create Laravel controllers to handle these requests effectively.
What is a Controller in Laravel?
In Laravel, a controller is a class that is responsible for handling user requests. It receives input from the user, processes it, and returns a response. This separation of concerns allows for cleaner and more maintainable code.
Setting Up Your Laravel Project
Before we dive into handling POST requests, ensure you have a Laravel project set up. If you haven't done this yet, you can create a new project using Composer:
composer create-project --prefer-dist laravel/laravel my-laravel-app
After the installation, navigate to your project directory:
cd my-laravel-app
Creating a Form
Let's create a simple form that we will use to submit data via a POST request. Create a blade view file named form.blade.php in the resources/views directory.
<!-- resources/views/form.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Laravel Form</title>
</head>
<body>
<h1>Submit Your Data</h1>
<form action="{{ route('form.submit') }}" method="POST">
@csrf
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br>
<button type="submit">Submit</button>
</form>
</body>
</html>
Explanation
@csrf: This blade directive generates a CSRF token to protect your application from cross-site request forgery attacks.- The
actionattribute of the form points to a named routeform.submit, which we will define in our route file.
Creating the Controller
Next, we need to create a controller that will handle the form submission. You can do this using the Artisan command-line tool.
php artisan make:controller FormController
This command creates a new file named FormController.php in the app/Http/Controllers directory. Open this file and add the following code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormController extends Controller
{
public function showForm()
{
return view('form');
}
public function handleSubmit(Request $request)
{
// Validate the incoming request data
$request->validate([
'name' => 'required|string|max:255',
]);
// Process the data (e.g., save to database or send email)
$name = $request->input('name');
// Return a response or redirect
return redirect()->back()->with('success', 'Form submitted successfully, ' . $name . '!');
}
}
Explanation
- The
showFormmethod returns the form view. - The
handleSubmitmethod processes the form data. It validates the input and can then handle the data as needed, such as saving it to a database.
Defining Routes
Now, we need to define the routes that will handle displaying the form and submitting it. Open the routes/web.php file and add the following code:
use App\Http\Controllers\FormController;
Route::get('/form', [FormController::class, 'showForm'])->name('form.show');
Route::post('/form-submit', [FormController::class, 'handleSubmit'])->name('form.submit');
Explanation
- The first route displays the form when the user navigates to
/form. - The second route handles the POST request when the form is submitted.
Testing the Form Submission
Now that everything is set up, start your Laravel development server:
php artisan serve
Visit http://localhost:8000/form in your web browser. You should see the form we created. Enter a name and click "Submit."
If everything is working correctly, you should receive a success message indicating that the form was submitted successfully.
Conclusion
In this tutorial, we covered the basics of handling HTTP POST requests in Laravel using controllers. You learned how to create a form, define routes, and process form submissions. This is just the beginning; Laravel offers many more features for developing robust web applications.
Feel free to expand on this by adding features such as database storage, validation messages, and more complex forms. Happy coding!
Connect with SkillBakery Studios
Explore more tutorials, tools, and resources:
Posted by SkillBakery Studios


No comments:
Post a Comment