← Other topics

Laravel In a Nutshell - Controllers (#3)

Series contents:

Video Notes

In the last video in this series (Routes), we set up a new route to respond to incoming requests matching the URL pattern of /products/{category}. As part of that route, we defined a function that contained code to handle the request.

We could continue down this path, defining all the functionality for our routes within /web/routes.php, but you can imagine how that could quickly become cumbersome to have to program all the logic of our application in a single file.

Knowing this, we want to delegate handling incoming requests from our routes file to Controllers. In Laravel, Controllers are PHP classes that we use to organize the logic necessary to respond to incoming requests to our application.

As an example, in the video I create a ProductsController class to handle the work I've done so far with my /products/{category} route.

To generate this controller, I use Artisan, Laravel’s built in command line utility:

> php artisan make:controller ProductsController

The resulting file is found at /app/Http/Controllers/ProductsController.php and within it, I add a method showProductsByCategory. Within this method, I copy over the code I had previously written in my routes file to filter the products and return a View:


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProductsController extends Controller
    public function showProductsByCategory($category)
        $productsByCategory = [
            'health' => [
                'Johnson’s Baby Powder',
            'tech' => [
                'GoPro Action Camera',
                'FitBit Fitness Watch',
                'Nintendo Switch'
            'books' => [
                'The Martian',
                'The Great Gatsby',
                'Joy Luck Club'

        $products = $productsByCategory[$category];
        return view('products')->with('products', $products)->with('category', $category);


Finally, I update my /products/{category} route so that instead of invoking a function, it invokes the showProductsByCategory method within my new ProductsController class:


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductsController;

Route::get('/', function () {
    return view('welcome');

Route::get('/products/{category}', [ProductsController::class, 'showProductsByCategory']);

The end result is the page loads exactly the same as it did previously, but our code is better organized, setting is up for our application to grow.


That’s Controllers in a Nutshell! To learn more about Controllers, skim the documentation at https://laravel.com/docs/controllers

If this info helped you out you can say thanks and support future content by clicking my Amazon affiliate link: https://amzn.to/3UtgnYk. If you make a purchase on Amazon within 24 hours of clicking the link I may receive a micro-commission and it costs you nothing extra. Any income from these sales goes directly to supporting me in making new videos and guides. Thank you for your support!

← Other topics