Skip to content

Instantly share code, notes, and snippets.

@blood72
Last active June 25, 2019 06:31
Show Gist options
  • Save blood72/3f07e06a380bb82ef1ea8e850d60670f to your computer and use it in GitHub Desktop.
Save blood72/3f07e06a380bb82ef1ea8e850d60670f to your computer and use it in GitHub Desktop.
Route::file, Route::directory macro with route_path() helper in Laravel
<?php
use Illuminate\Support\Facades\Route;
if (! Route::hasMacro('directory')) {
Route::macro('directory', function ($path, $attributes = [], $recursive = false) {
$route = $this; // for IDE
/** @var Route $route */
return $route->group((array) $attributes, function () use ($path, $recursive) {
foreach (File::{$recursive ? 'allFiles' : 'files'}($path) as $file) {
require $file;
}
});
});
}
<?php
use Illuminate\Support\Facades\Route;
if (! Route::hasMacro('file')) {
Route::macro('file', function ($file, $attributes = []) {
$route = $this; // for IDE
/** @var Route $route */
return $route->group((array) $attributes, function () use ($file) {
require $file;
});
});
}
<?php
if (! function_exists('route_path')) {
/**
* Get the path to the route folder.
*
* @param string $path
* @return string
*/
function route_path($path = '')
{
return base_path('routes').($path ? DIRECTORY_SEPARATOR.$path : $path);
}
}
@blood72
Copy link
Author

blood72 commented Jun 23, 2019

Introduce

This method allows you to freely group and add subdirectories of routes path.
Illuminate\Routing\RouteRegistrar is better positioned than Illuminate\Routing\Router.
But RouteRegistrar does not currently support macros (current version is Laravel 5.8.24).
It is similar to laravel/ideas#139

Usage

Route::directory($path, $attributes = [], $recursive = false)

The parameters receive the absolute path of the file, the attributes properties of the group, and whether to apply recursive the folder.
It can be set using like this method instead of forwarding attributes maybe if added to RouteRegistrar.

for example,

Route::prefix()->namespace()->middleware()->directory();

or in app\Providers\RouteServiceProvider.php

protected function mapWebRoutes()
{
    Route::middleware('web')
        ->namespace($this->namespace)
        ->directory(base_path('routes/web'));
}

Example

in routes/web.php

<?php

Route::directory(route_path('web'));

Route::directory(__DIR__ . '/admin', ['prefix' => 'admin', 'middleware' => 'auth'], true);

Then, all the sub-files in the routes/web folder will be grouped and added.
In addition, all subfolders and files in routes/admin will be affected to auth middleware.

This method can be used for all sub routes (if $recursive is true, there is a risk of duplicating).

You can also set up mapping using RouteServiceProvider.

protected function mapWebRoutes()
{
    // Route::middleware('web')
    //     ->namespace($this->namespace)
    //     ->group(base_path('routes/web.php'));
    Route::directory(base_path('routes/web'), [
        'namespace' => $this->namespace,
        'middleware' => 'web',
    ]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment