Adding controllers and views - McNamara84/ladis GitHub Wiki

A quick start guide for adding new controllers.

Tip

  • Troubleshooting is easier if you commit and push after every step.

Step 1: Generating a new controller file

Run php artisan make:controller CONTROLLERNAMEController to generate a new, empty controller file.

Step 2: Generating a new view file

Run php artisan make:view VIEWNAME to generate a new, empty view file.

Step 3: Adding index() method

To ensure that the view is displayed as soon as the new controller is called, we should create a small method called index():

public function index()
    {
        return view('changelog');
    }

Step 4: Adding a route to the controller

Open the file routes\web.php and add a new route. In my case I would like to make a route for /changelog:

Route::get('/changelog', [ChangelogController::class, 'index']);

Note

When we now call localhost:8000/changelog, this route calls the index() method in the controller (in my case, ChangelogController). Since we only wrote in index() that the new view should be called, our newly created view is now displayed at localhost:8000/changelog.

Step 5: Editing the view

The view resources\views\layouts\app.blade.php specifies cross-page elements (e.g. logo, navigation bar, etc), while our new view resources\views\changelog.blade.php contains the actual content of our new page. Now let's customize this content a bit, as it is currently empty:

@extends('layouts.app')

@section('title', 'Changelog - Laser-Projekt - FH Potsdam')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-10">
            <div class="card">
                <div class="card-body">
                    <h1 class="card-title">Changelog</h1>
                    <p class="card-text">Welcome to our changelog page!</p>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Note

For now, we will ignore lines that contain @extends, @section, and @endsection, for example. These are Blade directives that we will discuss later. Blade is Laravel's template engine.

Step 6: Pass Data to the View

Basically, we have now created a stylish new subpage and can already access it in the browser. However, if we write all the data directly into the view, we will only have a static page. We would then have to regularly adjust the content in the HTML structure of the view under resources\views\changelog.blade.php. To enable the data to be generated dynamically in the controller, we will next edit our controller and have it pass a few variables to the view:

public function index()
{
   $pageTitle = 'Project Changelog';
   $lastUpdated = '2025-06-13';

   return view('changelog', compact('pageTitle', 'lastUpdated'));
}

Step 7: Showing variables in the view

The newly defined variables are now passed from the controller to the view, but the view does not display them because we have not yet placed placeholders for displaying these two pieces of data in the view. We will do that now and adjust our view under resources\views\changelog.blade.php:

@extends('layouts.app')

@section('title', 'Changelog - Laser-Projekt - FH Potsdam')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-10">
            <div class="card shadow">
                <div class="card-body">
                    <h1 class="card-title text-primary">{{ $pageTitle }}</h1>
                    <p class="text-muted small mb-4">Last updated: {{ $lastUpdated }}</p>
                    <p class="card-text">Welcome to our changelog page!</p>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Note

The page should now display the dynamic title and last updated date with Bootstrap styling. Check via development server.

⚠️ **GitHub.com Fallback** ⚠️