Compartmentalized Routing

Submitted by rcoble - 9 months ago

An easy way to organize a large number of routes.

// Setup
// 1. Create a directory named "Routers" within the "app/Http" directory.
// 2. Create a file named "RouterInterface.php" within the "Routers" directory.
// 3. Create files for each division of routes. EX: "AuthRouter.php"


// Router interface (RouterInterface.php)
namespace App\Http\Routers;

interface RouterInterface {
    public static function setRoutes();
}


// AuthRouter (AuthRouter.php)
namespace App\Http\Routers;

use Route; // Route facade (Illuminate\Support\Facades\Route)

class AuthRouter implements RouterInterface {
    public static function setRoutes() {
        
        // login routes
        Route::get('login', 'AuthController@login');
        Route::post('login', 'AuthController@doLogin');
        
        // logout route
        Route::get('logout', 'AuthController@logout');
    }
}


// routes.php
// reference all your routers
use App\Http\Routers\AuthRouter;

// use your routers
AuthRouter::setRoutes();

comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks