php - 为 Lumen 中的所有路线添加前缀

标签 php routes lumen

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/

相关文章:

php - 显示3个表的数据

php - SQL 查询 - 获取行退出其他表 laravel 上的一些值

未读取 C# MVC url 参数

ruby-on-rails - 表单提交的值为 nil(缺少参数或值为空 : amount)

php - guzzle NULL响应lumen php oauth2

php - Braintree 推送通知不起作用

php - 哪种操作系统最适合 PHP 开发或一般开发?

routes - 使用 asp.net 4.0 路由功能从 httphandler 永久重定向页面

vue.js - VueJS 如何每 x 秒调用一次 API(聊天)

mysql - 最后插入的 id 与 Lumen/Laravel 的内部交易