Created
November 17, 2019 21:49
-
-
Save sen0rxol0/0173d5e7b4b52529b9388ee67b5e1a10 to your computer and use it in GitHub Desktop.
Laravel routes with simple code
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 | |
// routes/web.php | |
$routesV1 = [ | |
'methods' => [ | |
['name' => 'get', 'handlers' => ['index', 'show']], | |
['name' => 'post', 'handlers' => ['store']], | |
['name' => 'put', 'handlers' => ['update'], 'params' => 'id'], | |
['name' => 'delete', 'handlers' => ['delete'], 'params' => 'id'], | |
], | |
'middleware' => 'auth:api', | |
'routes' => [ | |
'books' => [ | |
'controller' => 'V1\BooksController' | |
], | |
'favorites' => [ | |
'controller' => 'V1\FavoritesController' | |
] | |
] | |
]; | |
Route::group(['middleware' => $routesV1['middleware']], function () use ($routesV1) { | |
foreach ($routesV1['routes'] as $baseName => $route) { | |
foreach ($routesV1['methods'] as $method) { | |
foreach ($method['handlers'] as $handler) { | |
$routeHandler = "{$route['controller']}@{$handler}"; | |
$routePath = isset($method['params']) ? "/{$baseName}/{{$method['params']}}" : "/{$baseName}"; | |
$routeName = "{$baseName}.{$handler}"; | |
call_user_func_array([ | |
Route::class, "{$method['name']}" | |
], array($routePath, $routeHandler))->name($routeName); | |
} | |
// Route::get('/books/{id}', 'V1\BooksController@show')->name('books.show'); | |
// Route::post('/books', 'V1\BooksController@store')->name('books.store'); | |
// Route::put('/books/{id}', 'V1\BooksController@update')->name('books.update'); | |
// Route::delete('/books/{id}', 'V1\BooksController@delete')->name('books.delete'); | |
} | |
} | |
}); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment