Last active
June 25, 2019 06:31
-
-
Save blood72/3f07e06a380bb82ef1ea8e850d60670f to your computer and use it in GitHub Desktop.
Route::file, Route::directory macro with route_path() helper in Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
}); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
}); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Introduce
This method allows you to freely group and add subdirectories of
routes
path.Illuminate\Routing\RouteRegistrar
is better positioned thanIlluminate\Routing\Router
.But
RouteRegistrar
does not currently support macros (current version is Laravel 5.8.24).It is similar to laravel/ideas#139
Usage
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,
or in
app\Providers\RouteServiceProvider.php
Example
in
routes/web.php
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
.