thejawker /
laravel-route-module-macro
| 1 | <?php |
||
| 2 | |||
| 3 | namespace TheJawker\RouteModuleMacro; |
||
| 4 | |||
| 5 | use Illuminate\Support\Str; |
||
| 6 | use Illuminate\Routing\Router; |
||
| 7 | use Illuminate\Support\ServiceProvider; |
||
| 8 | |||
| 9 | class RouteModuleMacroServiceProvider extends ServiceProvider |
||
| 10 | { |
||
| 11 | public function register() |
||
| 12 | { |
||
| 13 | if (!Router::hasMacro('module')) { |
||
| 14 | $this->registerMacro(); |
||
| 15 | } |
||
| 16 | } |
||
| 17 | |||
| 18 | private function registerMacro() |
||
| 19 | { |
||
| 20 | Router::macro('module', function ($module, $only = [], $options = []) { |
||
| 21 | $onlyOptions = count($only) ? ['only' => $only] : []; |
||
| 22 | |||
| 23 | $controllerNameArray = collect(explode('.', $module))->map(function ($name) { |
||
| 24 | return Str::studly($name); |
||
| 25 | }); |
||
| 26 | |||
| 27 | $lastName = $controllerNameArray->pop(); |
||
| 28 | |||
| 29 | $controllerName = $controllerNameArray->map(function ($name) { |
||
| 30 | return Str::singular($name); |
||
| 31 | })->push($lastName)->push('Controller')->implode(''); |
||
| 32 | |||
| 33 | Router::resource($module, $controllerName, array_merge($onlyOptions, $options)); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 34 | }); |
||
| 35 | } |
||
| 36 | } |
||
| 37 |