Controllers organize the logic that handles incoming requests. Instead of writing all application logic inside routes, you can create controller methods to process requests, retrieve data, and return views or responses.
A controller is a PHP class that groups related request-handling methods.
For example, a StudentController might handle:
Controllers are usually stored in:
app/Http/Controllers/
Keeping application logic in controllers makes routes easier to read and maintain.
Laravel provides an Artisan command to create controllers.
php artisan make:controller StudentController
Laravel creates a file similar to:
app/Http/Controllers/StudentController.php
A basic controller class looks like this:
<?php
namespace App\Http\Controllers;
class StudentController extends Controller
{
//
}
The controller extends Laravel's base
Controller class.
Add a method inside StudentController to return a simple response.
<?php
namespace App\Http\Controllers;
class StudentController extends Controller
{
public function index()
{
return 'Student List';
}
public function create()
{
return 'Create New Student';
}
}
Here, index() and
create() are controller methods.
They can be connected to routes so that Laravel calls the appropriate method when a request arrives.
Open routes/web.php and import
StudentController.
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StudentController;
Route::get('/students', [StudentController::class, 'index']);
Route::get('/students/create', [StudentController::class, 'create']);
Now visit:
/students — displays Student List./students/create — displays Create New Student.The array contains the controller class and the method Laravel should call.
You can check registered routes with:
php artisan route:list
Controllers can return Blade views instead of plain text.
Inside StudentController.php:
public function index()
{
return view('students');
}
Create a file named
resources/views/students.blade.php.
<!DOCTYPE html>
<html>
<head>
<title>Students</title>
</head>
<body>
<h1>Student Management</h1>
<p>Welcome to the student list.</p>
</body>
</html>
Keep the route connected to the controller:
Route::get('/students', [StudentController::class, 'index']);
Laravel will render the Blade template when the
visitor opens /students.
You can pass variables from a controller to a Blade view.
Controller method:
public function show()
{
$name = 'Rahul';
$course = 'Laravel';
return view('student', compact('name', 'course'));
}
Create resources/views/student.blade.php.
<h1>Student Details</h1>
<p>Name: {{ $name }}</p>
<p>Course: {{ $course }}</p>
Register the route:
Route::get('/student-details', [StudentController::class, 'show']);
The compact() function creates an
associative array containing the specified variables.
A controller method can receive parameters from the route.
Inside StudentController.php:
public function showById($id)
{
return 'Student ID: ' . $id;
}
Register the route:
Route::get('/student/{id}', [
StudentController::class,
'showById'
])->whereNumber('id');
Open /student/101 to get:
In a real application, use the ID to retrieve the corresponding student record from the database.
A resource controller provides conventional methods for CRUD operations: create, read, update, and delete.
Create a resource controller:
php artisan make:controller StudentController --resource
Laravel generates these conventional methods:
| Method | Purpose |
|---|---|
index() |
Display a list of records. |
create() |
Show a form to create a record. |
store() |
Save a new record. |
show() |
Display one record. |
edit() |
Show an edit form. |
update() |
Update an existing record. |
destroy() |
Delete a record. |
Register resource routes:
Route::resource('students', StudentController::class);
This registers conventional routes for the resource.
Use php artisan route:list to inspect
their HTTP methods, URLs, and names.
Resource routes do not automatically implement database operations; you must write the controller methods.
Practise creating and using Laravel controllers.
StudentController using Artisan.index() method that returns a student list message.create() method that returns a form page message.students.blade.php.php artisan route:list to inspect your routes.Tip: Keep routes focused on URL mapping and put request-handling logic in controllers.
1. Where are Laravel controllers usually stored?
2. Which command creates a controller?
3. Which method registers conventional CRUD routes?