我们应该如何构建REST API?
示例结构
app/
v1/
controllers/
c1_controller
c2_controller
models/
m1
m2
views/
view1
view2
v2/
controllers/
c1_controller
c2_controller
models/
m1
m2
views/
view1
view2
如何将CRUD添加到此结构中?
CRUD ===行动?
答案 0 :(得分:2)
你在问题中提到的是一个简单的MVC结构。基于REST的应用程序的目录结构不必与MVC布局不同。
要使您的应用程序RESTful,您需要根据动词在控制器中添加操作。
如果你有一个UserController,你将有四个动作 - GET,POST,PUT,DELETE
POST v1/user/ --would create a new user
GET v1/user/ --would return all users
PUT v1/user/ --would update all users
DELETE v1/user/ --would delete all users
POST v1/user/123 --would do nothing or throw error
GET v1/user/123 --would return User with ID 123
PUT v1/user/123 --would update User with ID 123
DELETE v1/user/123 --would delete User with ID 123
当您使用Zend Framework等框架构建应用程序时,它会根据方法将您的URL重定向到适当的操作。