How should we structure our REST APIs?
Example structure
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
How would you add CRUD to this structure?
CRUD === Actions?
What you have mentioned in the question is a plain MVC structure. Directory structure for REST based apps need not be different from MVC layout.
To make your app RESTful, you would need to add actions in your controllers based on verbs.
If you have a UserController, you would have four actions - 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
When you build you app using a framework like Zend Framework, it will redirect your URL to appropriate actions based on the method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With