How to Get to Know Your Controller: A Basic Guide


🧠 What is a Laravel Controller?
Imagine you're building a website where users can view, add, or delete products. The Controller is like the manager that handles these tasks. It takes requests from the user, processes them, and returns the appropriate response.
🛠️ Step-by-Step Guide to Working with Laravel Controllers
1. Creating a Controller
To create a controller, open your terminal and run:
php artisan make:controller ProductController
This command creates a new file named ProductController.php
in the app/Http/Controllers
directory.
2. Defining Methods in the Controller
Open the ProductController.php
file and define methods to handle different actions:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
return 'Display all products';
}
public function create()
{
return 'Show form to create a new product';
}
public function store(Request $request)
{
return 'Store new product';
}
public function show($id)
{
return "Display product with ID: $id";
}
public function edit($id)
{
return "Show form to edit product with ID: $id";
}
public function update(Request $request, $id)
{
return "Update product with ID: $id";
}
public function destroy($id)
{
return "Delete product with ID: $id";
}
}
Each method corresponds to a specific action related to products.
3. Setting Up Routes
Next, we need to tell Laravel which URL should trigger each method. Open the routes/web.php
file and add:
use App\Http\Controllers\ProductController;
Route::resource('products', ProductController::class);
This single line automatically sets up routes for all the methods in your controller.
4. Creating Views
For each action, you'll want to display a view. Create Blade templates in the resources/views/products
directory:
index.blade.php
: Display all products.create.blade.php
: Form to create a new product.edit.blade.php
: Form to edit an existing product.show.blade.php
: Display a single product.
5. Handling Form Submissions
In the store
and update
methods, you'll handle form submissions. Use the Request
object to access the submitted data:
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|max:255',
'price' => 'required|numeric',
]);
// Store the product
}
This ensures the data is valid before storing it.
6. Displaying Data
In the index
method, you can retrieve all products from the database and pass them to the view:
public function index()
{
$products = Product::all();
return view('products.index', compact('products'));
}
In your index.blade.php
:
@foreach ($products as $product)
<p>{{ $product->name }} - ${{ $product->price }}</p>
@endforeach
7. Redirecting After Actions
After performing actions like storing or updating, it's a good practice to redirect the user:
return redirect()->route('products.index');
This sends the user back to the list of products.
8. Handling Deletions
In the destroy
method, you can delete a product:
public function destroy($id)
{
Product::destroy($id);
return redirect()->route('products.index');
}
User makes a request (e.g., clicks a link).
Route maps the request to a controller method.
Controller processes the request and interacts with the Model.
Model retrieves or manipulates data.
Controller returns a View to the user.
✅ Summary
Controllers handle user requests and return responses.
Use
php artisan make:controller
to create a new controller.Define methods for each action (index, create, store, etc.).
Set up routes in
web.php
to link URLs to controller methods.Create Blade views to display data to the user.
Use the
Request
object to handle form submissions and validate data.Redirect users after actions to improve user experience.
Subscribe to my newsletter
Read articles from Nature directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
