Lumen 有没有办法为我的所有路线添加前缀?
问题是,我通过 URI 对 API 进行版本控制,对于我创建的每个组,我必须将前缀设置为“v1/*”,如下所示:
$app->group(['prefix' => 'v1/students/', 'namespace' => 'App\Http\Controllers\Students\Data'], function () use ($app) {
$app->get('/', 'StudentController@get');
$app->get('/{id}', 'StudentController@getByID');
});
最佳答案
显然,Lumen 中的路由组不会继承任何设置,这是为了使路由器更简单、更快( see comment here )。
您最好的选择可能是为每个版本创建一个路由组,以便为该版本定义基本前缀和 Controller 命名空间。但是,这些路由组中的各个路由需要稍微详细一些。示例如下:
// creates v1/students, v1/students/{id}
$app->group(['prefix' => 'v1', 'namespace' => 'App\Http\Controllers'], function () use ($app) {
$app->get('students', 'Students\Data\StudentController@get');
$app->get('students/{id}', 'StudentController@getByID');
});
// creates v2/students, v2/students/{id}, v2/teachers, v2/teachers/{id}
$app->group(['prefix' => 'v2', 'namespace' => 'App\Http\Controllers'], function () use ($app) {
$app->get('students', 'Students\Data\StudentController@get');
$app->get('students/{id}', 'Students\Data\StudentController@getByID');
$app->get('teachers', 'Teachers\Data\TeacherController@get');
$app->get('teachers/{id}', 'Teachers\Data\TeacherController@getByID');
});
关于php - 为 Lumen 中的所有路线添加前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39454204/