How to write dynamic url method in laravel routes file?

I have a list of routes mentioned below for your reference. I have to do it as dynamically.

Example:

First set of routes

Route::get('artists/info', 'ArtistApiController@getInfo');
Route::post('artists/records', 'ArtistApiController@postRecords');
Route::post('artists/action', 'ArtistApiController@postAction');
Route::post('artists', 'ArtistApiController@postAdd');
Route::put('artists/update/{id}', 'ArtistApiController@postAdd');
Route::post('artists/update-status/{id}', 'ArtistApiController@postUpdateStatus');

Second set of routes

Route::get('albums/info', 'AlbumApiController@getInfo');
Route::post('albums/records', 'AlbumApiController@postRecords');
Route::post('albums/action', 'AlbumApiController@postAction');
Route::post('albums', 'AlbumApiController@postAdd');
Route::put('albums/update/{id}', 'AlbumApiController@postAdd');
Route::post('albums/update-status/{id}', 'AlbumApiController@postUpdateStatus');

Both routes seem to be the same except for the keyword "albums, artists, AlbumApiController, ArtistApiController". Therefore, I need to do this dynamically in order to reuse this code by passing the necessary parameters. To make it look lower or regardless of syntax, let it be.

grid('albums', 'AlbumApiController');

Thanks in advance

+4
source share
2 answers

You can do something like this:

foreach (['artists' => 'ArtistApiController', 'albums' => 'AlbumApiController'] as $route => $controller) {
    Route::get($route . '/info', $controller . '@getInfo');
    Route::post($route . '/records', $controller . '@postRecords');
    Route::post($route . '/action', $controller . '@postAction');
    Route::post($route, $controller . '@postAdd');
    Route::put($route . '/update/{id}', $controller . '@postAdd');
    Route::post($route . '/update-status/{id}', $controller . '@postUpdateStatus');
}
+2
source

, ? , , ... , .

+2

Source: https://habr.com/ru/post/1691419/


All Articles