$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[September 23, 2024] Laravel

Laravel Folio: File-Based Routing

Laravel Folio: File-Based Routing

────────────────────────────────────────────────────────

Laravel Folio: File-Based Routing

Tip: Install Folio

composer require laravel/folio
php artisan folio:install

Gotcha: File Structure = Routes

resources/views/pages/posts/index.blade.php/posts resources/views/pages/posts/[id].blade.php/posts/{id}

Tip: Page Middleware

use function Laravel\Folio\middleware;

middleware(['auth']);

Place at the top of any page file.

Gotcha: Inline PHP Logic

@php
    $posts = Post::latest()->get();
@endphp

Folio pages can contain PHP logic directly.

Tip: Named Routes

use function Laravel\Folio\name;

name('posts.index');

Gotcha: Folio vs Traditional Routes

Folio is great for simple apps. Complex routing still needs traditional Route:: definitions.

Tip: Use route:cache Carefully

php artisan route:cache is fast, but it doesn't work with closure-based routes. Every time you cache routes, Laravel serializes them. If you have Route::redirect() or closure callbacks, the cache breaks. Stick to controller-based routes in production.

Tip: Model APP_KEY Rotation

Rotating APP_KEY invalidates all encrypted data — cookies, encrypted DB columns, and password reset tokens. If you must rotate (e.g., after a leak), plan a migration that re-encrypts existing data with the new key.

Gotcha: Local Scope Leaks

Global scopes defined in booted() apply to ALL queries on that model — including relationships. An innocent User::all() in admin panel might exclude soft-deleted users if a global scope is active.

Senior Insight

File-based routing is convenient but creates a tight coupling between URL structure and file organization. I've refactored projects where changing a URL meant renaming files, which confused the version control history and broke permalinks. Folio is great for simple sites, but for complex applications with evolving URL structures, traditional route definitions give you more control and better visibility into your routing table.

Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)

────────────────────────────────────────────────────────
<-- back to posts