Laravel API控制器结构? [英] Laravel API controller structure?

查看:92
本文介绍了Laravel API控制器结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于用户api来说,这种控制器结构哪一个有意义?

Which one this controller structure for user api make sense

每个API版本的UI和API的单独控制器

/app/controllers/UsersController.php
/app/controllers/api/v1/ApiUsersController.php

用于UI和API的单独控制器,并通过代码处理版本控制

/app/controllers/UsersController.php
/app/controllers/api/ApiUsersController.php 

使用单个控制器,检测路由器内的/api/调用.根据网址返回html/json.

/app/controllers/UsersController.php  

推荐答案

当然,第一个选择是这三个中最好的,其原因是可扩展性维护 .

Definitely the first option is the best out of the three, and the reasons are scalability and maintenance.

如果仅使用第一种方法中建议的一个控制器,则很快就会出现一个混乱的类,其中包含数千行.这不仅不能很好地扩展规模,而且对您和您的队友也很难与之合作.

If you use only one controller, as proposed in the third method, you will end up soon with a messy class with thousands of lines. Not only this is not going to scale well, but it will be hard to work with for you and your teammates.

第二个选择比第三个选择要好,但仍然不是最好的.从一开始就支持API版本控制是一种更好的做法,它将使您的路由,文件和名称空间更有条理.

The second choice is better than the third one, but still not the best. It's a better practice to be supporting API versioning from the start, and it will keep your routes, files and namespaces more organized.

顺便说一句,只要命名正确,也可以使用UserController而不是将其命名为ApiUserController.因此,您可以同时存在\UserControllerApi\V1\UserController.

By the way, instead of naming it ApiUserController you can use UserController too, as long as you are namespacing properly. So, you could have \UserController and Api\V1\UserController existing together.

这篇关于Laravel API控制器结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆