I know there's a way to create versioned URLs for REST APIs with routes, but what's the best way to organize controllers and controller files? I want to be able to create new versions of APIs, and still keep the old ones running for at least some time.

推荐答案

I ended up using namespaces and direct或ies under app/controllers:

/app
  /controllers
    /Api
      /v1
        /UserController.php
      /v2
        /UserController.php

And in UserController.php files I set the namespace acc或dingly:

namespace Api\v1;

namespace Api\v2;

然后在我的路由上,我做了这样的事情:

Route::group(['prefix' => 'api/v1'], function () {
  Route::get('user',      'Api\v1\UserController@index');
  Route::get('user/{id}', 'Api\v1\UserController@show');
});

Route::group(['prefix' => 'api/v2'], function () {
  Route::get('user',      'Api\v2\UserController@index');
  Route::get('user/{id}', 'Api\v2\UserController@show');
});

我不确定这是最好的解决方案.然而,它允许以不相互干扰的方式对控制器进行版本控制.如果需要的话,你可以用模型做一些类似的验证.

Laravel相关问答推荐

Laravel:如何从不断更新的Controller中分页数据

如何让 Laravel 的 Collection 表现得像一个流?

Laravel Horizo​​n 限制与优化

未找到 apiResource 404 中的变量

如何防止 Laravel 路由被直接访问(即非 ajax 请求)

Eloquent的条件过滤器

Laravel 错误ReflectionException-类 App\Http\Kernel 不存在

Laravel belongsToMany 关系在两个表上定义本地键

Laravel - 排序的集合输出不是数组

Laravel 5 迁移:Schema::table 方法中不同类型的默认长度

laravel 中的 Http Post 使用 fetch api 给出 TokenMismatchException

Laravel 5 - 在控制器文件中为多个路由定义中间件

使用模型工厂、一对一和一对多关系定义 Laravel 外键,而不创建不必要的模型

Laravel 动作未定义

如何在 Laravel 5.1 中为Electron邮件添加标题

干预图像:直接从带有原始文件名和分机的网址保存图像?

Laravel 搜索关系

如何在 Eloquent Orm 中实现自引用(parent_id)模型

Laravel:array_merge():参数#2不是数组错误

基于日期的 Laravel 日志(log)文件