我有一堆路线列表,如下所述,供您参考.我必须动态地制作它.
例:
第一套路线
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');
第二套路线
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');
除了关键字“专辑,艺术家,AlbumApiController,ArtistApiController”之外,似乎路线列表都相同.所以我需要动态地创建它,以便通过传递所需的参数来重用该代码.所以它看起来像下面或任何语法让它成为.
grid('albums', 'AlbumApiController');
提前致谢
解决方法:
你可以这样做:
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');
}