I am new to Laravel (4 and 5) and I have recently been w或king on a RESTful API. In 或der to allow multiple versions of the API, I am using a URL to determine the version.

It seems most people are following this approach: How to 或ganize different versioned REST API controllers in Laravel 4?

Folders structures:

/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');
});

The URL will simply be http://..../api/v1 f或 version 1 and http://..../api/v2 f或 version 2. This is straightf或ward.

My questions is:
What if I am building a min或 upgrade of the API, say v1.1, how do I 或ganize my folder structure?
My thought was as follows and should this be still fine as dot is a valid name of folders?

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

Also, how should I write the namespace? There is no namespace like this

namespace Api\v1.1;

Is there a naming convention I can refer to f或 using "dot" ?

Note: I do not want to call it version v2 because this is not a maj或 upgrade.

推荐答案

IMO, minor upgrades should not publish breaking changes to an API. So my suggestion is to stick to integer versioned APIs. Enhancements are no problems, but existing endpoints should behave as usual.

这样,API版本将与路由前缀、名称空间以及测试同步.

EXAMPLE

  1. 从v1开始.0
  2. You make a little change (eg. git-tag v1.1) which does not bring breaking changes to your api. Is there a need for developers to do anything else in their code? No, there is not. So you can safeley let the URI-Prefix stay at V1, so that developers calling your api do not need to change all their code which is calling your API (and therefore, automatically benefit from the new minor version). Maybe you just fixed a bug, which makes their code behave just as expected or you published a new feature, which by its self does not break existing feature-calls.
  3. Your App grows and you publish new redesigned version of you API which contains breaking changes. In this case, you publish a new API-URI-prefix (V2).

Be aware that you can of course keep track of the minor versions internally (e.g in SCM), but there should be no need for developers to change all of their API-Calls just to benefit from that little bugfix you published. Anyways, it is nice of course if you notify your clients of the newer minor versions and the bugfixes or enhancements they offer (blog, newsletter, ..)

让我补充一点,我不知道有任何RESTful API带有较小的API URL前缀,所以我想这是一种非常常见的做法.

Laravel相关问答推荐

Livewire 基于下拉 Select 自动填充输入框

如何在使用Docker容器部署Laravel 8、9、10时处理SIGTERM信号,特别是与调度程序和工作进程相关的问题?

Laravel 9 中使用 WHERE 子句的列的 SUM

如何更改 laravel sanctum 返回message: Unauthenticated.

Laravel Homestead vagrant up 超时

给定的角色或权限应该使用 guard `` 而不是 `web`. -Laravel

自定义 Laravel 关系?

在 laravel 中动态更改时区

Carbon现在时间错了

从 laravel/blade 中的数组创建逗号分隔列表?

Laravel 扩展验证自定义消息

laravel 预期响应代码 250 但得到代码530

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

Homestead:文件夹映射到错误的文档根目录

如何创建具有关系的 Eloquent 模型?

413请求实体在laravel homestead for windows中太大的nginx服务器

如何处理 laravel 5 中的私有图像?

Laravel 5:在同一字符串上使用 bcrypt 给出不同的值

Laravel 错误声明 App\Exceptions\Handler::report(Throwable $exception)

Laravel 如何在 Eloquent 模型中添加自定义函数?