Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory structure for REST APIs

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?

like image 940
kaulusp Avatar asked Feb 16 '26 00:02

kaulusp


1 Answers

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.

like image 57
Lenin Raj Rajasekaran Avatar answered Feb 19 '26 04:02

Lenin Raj Rajasekaran