Lesson 10 of 50 – Laravel Controllers
20%

Laravel Controllers

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.

1. What Is a Controller?

A controller is a PHP class that groups related request-handling methods.

For example, a StudentController might handle:

  • Displaying student details.
  • Showing the student list.
  • Creating a new student.
  • Updating student information.
  • Deleting a student record.

Controllers are usually stored in:

app/Http/Controllers/

Keeping application logic in controllers makes routes easier to read and maintain.

2. Create a Controller Using Artisan

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.

3. Create a Controller Method

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.

4. Connect a Controller to a Route

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
5. Return a Blade View from a Controller

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.

6. Pass Data from a Controller to a View

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.

7. Controller Methods with Route Parameters

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:

Student ID: 101

In a real application, use the ID to retrieve the corresponding student record from the database.

8. Resource Controllers

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.

9. Practical Assignment

Practise creating and using Laravel controllers.

  1. Create a StudentController using Artisan.
  2. Add an index() method that returns a student list message.
  3. Add a create() method that returns a form page message.
  4. Connect both methods to routes.
  5. Create a Blade view named students.blade.php.
  6. Pass a student name and course name to a view.
  7. Create a method that receives a student ID from the URL.
  8. Create a resource controller and register resource routes.
  9. Run php artisan route:list to inspect your routes.

Tip: Keep routes focused on URL mapping and put request-handling logic in controllers.


🧠 Quick Quiz

1. Where are Laravel controllers usually stored?




2. Which command creates a controller?




3. Which method registers conventional CRUD routes?