Route Tips

Route Tips

Here are a few tips and techniques for your routes file to help keep it clean, maintainable and efficient.

1. Route Model Binding

Instead of manually fetching models by ID in your route closures, Laravel offers route model binding to automatically inject model instances based on route parameters.

For example, change this:

PHP
Route::get('/jobs/{id}', function ($id) {
    $job = Job::findOrFail($id);
    // ...
});

To this:

PHP
Route::get('/jobs/{job}', function (Job $job) {
    // $job is automatically resolved by Laravel
});

Key points:

  • The route parameter name ({job}) must match the variable name in the closure.
  • Laravel uses the model’s primary key (id by default) to fetch the record.
  • You can specify a different column (e.g., slug) by adding :slug to the route parameter.

Update all your routes to use route model binding for cleaner code.

2. Dedicated Controller Classes

For larger applications, managing many routes with closures becomes unwieldy. Instead, use controller classes to organize your route logic.

Generate a controller:

Bash
php artisan make:controller JobController

Move your route logic into controller methods like index, show, create, store, edit, update, and destroy.

Update your routes to reference controller actions:

PHP
use App\Http\Controllers\JobController;

Route::get('/jobs', [JobController::class, 'index']);
Route::get('/jobs/create', [JobController::class, 'create']);
// etc.

3. Route View Shortcut

For simple routes that only return a view (like static pages), use the Route::view method:

PHP
Route::view('/contact', 'contact');

This avoids creating unnecessary closures or controller methods.

4. Listing Routes

Use Artisan to list all your routes:

Bash
php artisan route:list

You can exclude vendor routes to focus on your app’s routes:

Bash
php artisan route:list --except-vendor

This helps you audit and manage your routes effectively.

5. Route Grouping with Controllers

To reduce repetition, group routes by controller:

PHP
Route::controller(JobController::class)->group(function () {
    Route::get('/jobs', 'index');
    Route::get('/jobs/create', 'create');
    // other routes...
});

This cleans up your routes file and makes it easier to maintain.

6. Route Resource

Laravel provides a convenient method to register all standard resource routes at once:

PHP
Route::resource('jobs', JobController::class);

This registers routes for index, create, store, show, edit, update, and destroy actions following RESTful conventions.

You can limit or exclude specific actions:

PHP
Route::resource('jobs', JobController::class)->except(['edit']);
Route::resource('jobs', JobController::class)->only(['index', 'show', 'create', 'store']);

Before – After

Here’s a before and after of my routes file

Before the Tips:

PHP
<?php

use Illuminate\Support\Facades\Route;
use App\Models\Job;

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

// Index
Route::get('/jobs', function () {
    $jobs = Job::with('employer')->latest()->simplePaginate(3);

    return view('jobs.index', [
        'jobs' => $jobs
    ]);
});

// Create
Route::get('/jobs/create', function () {
    return view('jobs.create');
});

// Show
Route::get('/jobs/{id}', function ($id) {
    $job = Job::find($id);

    return view('jobs.show', ['job' => $job]);
});

// Store
Route::post('/jobs', function () {
    request()->validate([
        'title' => ['required', 'min:3'],
        'salary' => ['required']
    ]);

    Job::create([
        'title' => request('title'),
        'salary' => request('salary'),
        'employer_id' => 1
    ]);

    return redirect('/jobs');
});

// Edit
Route::get('/jobs/{id}/edit', function ($id) {
    $job = Job::find($id);

    return view('jobs.edit', ['job' => $job]);
});

// Update
Route::patch('/jobs/{id}', function ($id) {
    request()->validate([
        'title' => ['required', 'min:3'],
        'salary' => ['required']
    ]);

    // authorize (On hold...)

    $job = Job::findOrFail($id);

    $job->update([
        'title' => request('title'),
        'salary' => request('salary'),
    ]);

    return redirect('/jobs/' . $job->id);
});

// Destroy
Route::delete('/jobs/{id}', function ($id) {
    // authorize (On hold...)

    Job::findOrFail($id)->delete();

    return redirect('/jobs');
});

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

After the Tips:

PHP
<?php

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

Route::view('/', 'home');
Route::view('/contact', 'contact');

// jobs
Route::resource('jobs', JobController::class);

As you can see it makes quite a big difference.

Tags: