Organize Routes with Route Partials

Submitted by jhauraw - 2 years ago

Separate routes for large applications into multiple smaller route partial files that are automatically loaded at runtime. Example 'app/routes.php' file.

<?php

/**
 * This file: app/routes.php
 *
 * 1. Create a 'routes' folder under 'app/'.
 * 2. Create a route partial file for each
 *    route group; store in 'routes' folder.
 * 3. Add the partial file basename to the
 *    partial map.
 */

/** Route Partial Map
=================================================== */

// ORDER MATTERS!
$route_partials = [
    
    // Global: Filters, Patterns
    'global',

    // Auth, Registration
    'auth',
    'passwords',
    'registration',

    // Users, Tenants
    'users',
    'tenants',

    // Role-specific UIs
    'admin',

    // Billing
    'billing',

    // Aggregate Root Entities
    'money-maker',

    // CMS, Content
    'pages',

    // Webhooks
    'webhooks',

    // WILDCARDS GO LAST!
    'wildcards'
];

/** Route Partial Loadup
=================================================== */

foreach ($route_partials as $partial) {

    $file = __DIR__.'/routes/'.$partial.'.php';

    if ( ! file_exists($file))
    {
        $msg = "Route partial [{$partial}] not found.";
        throw new \Illuminate\Filesystem\FileNotFoundException($msg);
    }

    require_once $file;
}

Ads cover the costs of maintaining Laravel-tricks

comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks