Introduction
Laravel’s Controllers and Routing system are the backbone of clean, maintainable web applications. In this tutorial, you’ll learn how to:
- Create standard controllers for custom logic.
- Use resource controllers for RESTful CRUD operations.
- Link controllers to Blade templates for dynamic views.
- Optimize routing for SEO and scalability.
By the end, you’ll build a book and author management system with SEO-friendly URLs and structured code. Let’s dive in!
Step 1: Set Up Controllers
1.1 Standard Controller (Non-Resource)
Create BookController
:
php artisan make:controller BookController
Add Methods in app/Http/Controllers/BookController.php
:
public function index() { $books = ['Book 1', 'Book 2']; // Example data return view('books.index', compact('books')); } public function show($id) { $book = "Book $id"; // Example data return view('books.show', compact('book')); }
1.2 Resource Controller
Generate AuthorController
:
php artisan make:controller AuthorController --resource
Add Logic in app/Http/Controllers/AuthorController.php
:
public function index() { $authors = ['Author 1', 'Author 2']; // Example data return view('authors.index', compact('authors')); } public function show($id) { $author = "Author $id"; return view('authors.show', compact('author')); }
Step 2: Create Blade Templates
2.1 Blade Files for Books
Create resources/views/books/index.blade.php
:
<!DOCTYPE html> <html> <head> <title>Books List</title> </head> <body> <h1>All Books</h1> <ul> @foreach ($books as $book) <li>{{ $book }}</li> @endforeach </ul> </body> </html>
Create resources/views/books/show.blade.php
:
<h1>Book Details</h1> <p>{{ $book }}</p>
2.2 Blade Files for Authors
Create resources/views/authors/index.blade.php
:
<!DOCTYPE html> <html> <head> <title>Authors List</title> </head> <body> <h1>All Authors</h1> <ul> @foreach ($authors as $author) <li>{{ $author }}</li> @endforeach </ul> </body> </html>
Create resources/views/authors/show.blade.php
:
<h1>Author Details</h1> <p>{{ $author }}</p>
Step 3: Define Routes
3.1 Routes for BookController
(Standard)
In routes/web.php
:
use App\Http\Controllers\BookController; // Standard Routes Route::get('/books', [BookController::class, 'index'])->name('books.index'); Route::get('/books/{id}', [BookController::class, 'show'])->name('books.show');
3.2 Routes for AuthorController
(Resource)
use App\Http\Controllers\AuthorController; // Resource Routes (Auto-generates CRUD routes) Route::resource('authors', AuthorController::class);
SEO Tip: Use name()
for routes to create consistent, readable URLs (e.g., books.index
).
Step 4: Test Your Application
4.1 View Registered Routes
php artisan route:list
4.2 Access Pages in Browser
- Books:
- Authors:
Step 5: Best Practices
5.1 SEO-Friendly Routing
- Use hyphens in URLs:
Route::get('/books/{id}-{slug}', [BookController::class, 'show']);
- Add meta tags in Blade templates:
<meta name="description" content="Book details: {{ $book }}">
5.2 Blade Layouts
Create a reusable layout (resources/views/layouts/app.blade.php
):
<!DOCTYPE html> <html> <head> <title>@yield('title')</title> @yield('meta') </head> <body> @yield('content') </body> </html>
Extend layouts in child views:
@extends('layouts.app') @section('title', 'Books List') @section('meta') <meta name="description" content="List of all books"> @endsection @section('content') <h1>All Books</h1> <!-- Content --> @endsection
Conclusion
You’ve now mastered Laravel Controllers, Routing, and Blade Templates! By combining standard and resource controllers with SEO-friendly URLs and reusable Blade layouts, you’re ready to build scalable, maintainable web apps.
Next Steps:
- Add forms for creating/editing books and authors.
- Implement database integration with Eloquent.
- Explore middleware for authentication.
Got questions? Drop a comment below! 🚀