📅 Week 13 - Day 3: Routes, Controllers & Views

Friday | Duration: 3 hours | Build Your First MVC App! 🎯

🎯 Learning Objectives

🛣️ Laravel Routes

Routes define how your application responds to HTTP requests. They map URLs to controllers or closures.

routes/web.php - Web Routes File

<?php use Illuminate\Support\Facades\Route; // Basic route - returns text Route::get('/', function () { return 'Welcome to Laravel!'; }); // Route returning view Route::get('/home', function () { return view('home'); }); // Route with parameter Route::get('/user/{id}', function ($id) { return "User ID: " . $id; }); // Route with optional parameter Route::get('/post/{id?}', function ($id = null) { if ($id) { return "Post ID: " . $id; } return "All Posts"; }); ?>

🎮 HTTP Methods in Routes

Method Purpose Example
GET Retrieve/display data Route::get('/students', ...)
POST Submit form data Route::post('/students', ...)
PUT/PATCH Update existing data Route::put('/students/{id}', ...)
DELETE Delete data Route::delete('/students/{id}', ...)

🎛️ Creating Controllers

Generate Controller with Artisan:

# Basic controller php artisan make:controller HomeController # Resource controller (with CRUD methods) php artisan make:controller StudentController --resource # Created: app/Http/Controllers/HomeController.php

Basic Controller Structure:

<?php // app/Http/Controllers/HomeController.php namespace App\Http\Controllers; use Illuminate\Http\Request; class HomeController extends Controller { // Method to display homepage public function index() { return view('home'); } // Method to display about page public function about() { return view('about'); } // Method with parameter public function show($id) { return view('student.show', ['id' => $id]); } } ?>

🔗 Connecting Routes to Controllers

<?php // routes/web.php use App\Http\Controllers\HomeController; // Route to controller method Route::get('/', [HomeController::class, 'index']); Route::get('/about', [HomeController::class, 'about']); Route::get('/student/{id}', [HomeController::class, 'show']); // Named routes (useful for redirects) Route::get('/contact', [HomeController::class, 'contact'])->name('contact'); ?>

👁️ Creating Views (Blade Templates)

View File Location:

resources/views/

Create home.blade.php:

<!-- resources/views/home.blade.php --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Home Page</title> <style> body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; } h1 { color: #fa709a; } </style> </head> <body> <h1>Welcome to Laravel!</h1> <p>This is your first Laravel page using MVC architecture.</p> <a href="/about">About Us</a> </body> </html>

📦 Passing Data to Views

Method 1: Array

<?php // In Controller public function index() { $name = "Ahmad"; $age = 25; return view('home', ['name' => $name, 'age' => $age]); } ?>

Method 2: Compact

<?php public function index() { $name = "Ahmad"; $age = 25; return view('home', compact('name', 'age')); } ?>

Method 3: With

<?php public function index() { return view('home') ->with('name', 'Ahmad') ->with('age', 25); } ?>

Display Data in View:

<!-- resources/views/home.blade.php --> <h1>Welcome, {{ $name }}!</h1> <p>You are {{ $age }} years old.</p> <!-- Blade escapes HTML by default for security --> <!-- To display raw HTML (be careful!): --> {!! $htmlContent !!}

💻 Complete MVC Example

1. Create Controller:

php artisan make:controller StudentController

2. Build Controller (app/Http/Controllers/StudentController.php):

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class StudentController extends Controller { public function index() { $students = [ ['id' => 1, 'name' => 'Ahmad Ibrahim', 'course' => 'Web Development'], ['id' => 2, 'name' => 'Fatima Yusuf', 'course' => 'Data Science'], ['id' => 3, 'name' => 'Ibrahim Musa', 'course' => 'UI/UX Design'], ]; return view('students.index', compact('students')); } public function show($id) { $student = [ 'id' => $id, 'name' => 'Ahmad Ibrahim', 'email' => 'ahmad@email.com', 'course' => 'Web Development', 'age' => 20 ]; return view('students.show', compact('student')); } } ?>

3. Define Routes (routes/web.php):

<?php use App\Http\Controllers\StudentController; Route::get('/', function () { return view('welcome'); }); Route::get('/students', [StudentController::class, 'index']); Route::get('/students/{id}', [StudentController::class, 'show']); ?>

4. Create View (resources/views/students/index.blade.php):

<!DOCTYPE html> <html> <head> <title>Students List</title> <style> body { font-family: Arial; max-width: 800px; margin: 50px auto; padding: 20px; } h1 { color: #fa709a; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { padding: 12px; text-align: left; border: 1px solid #ddd; } th { background: #fa709a; color: white; } a { color: #fa709a; text-decoration: none; } </style> </head> <body> <h1>Students List</h1> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Course</th> <th>Action</th> </tr> </thead> <tbody> @foreach($students as $student) <tr> <td>{{ $student['id'] }}</td> <td>{{ $student['name'] }}</td> <td>{{ $student['course'] }}</td> <td><a href="/students/{{ $student['id'] }}">View</a></td> </tr> @endforeach </tbody> </table> </body> </html>

✏️ Practice Exercises

  1. Create a HomeController with index() and about() methods
  2. Define routes for / and /about
  3. Create home.blade.php and about.blade.php views
  4. Pass your name and age to the view
  5. Create a StudentController with list and show methods
  6. Build students listing page with Blade @foreach

✅ Today's Deliverable

← Previous Course Home Next Week →