3

Let's say my API has the following routes:

GET /theaters
GET /theaters/:id
GET /theaters/:id/movies
GET /movies
GET /movies/:id
GET /movies/:id/theaters

And then I have the following controllers with the listed methods:

TheaterController
    list
    show

MovieController
    list
    show

I'm trying to decide in which controllers the remaining requests would best be handled from a strict RESTful perspective. Would this

TheaterController
    listMoviesByTheater (handles GET /theaters/:id/movies)

MovieController
    showTheatersByMovie (handles GET /movies/:id/theaters)

or this

TheaterController
    listByMovie (handles GET /movies/:id/theaters)

MovieController
    listByTheater (handles GET /theaters/:id/movies)

be more RESTful? If neither is more RESTful, which is a more common approach?

Basically, the question boils down to whether TheaterController should only provide access to Theater resources and MovieController should only provide access to Movie resources.

5

REST doesn't have anything to say about which controller services which URL.

From a purely hierarchical perspective, your first example makes more sense because the first (most general) level in the URL is being handled by its respective controller.

i.e.

/theaters/:id/movies -- belongs in the Theaters controller, and

/movies/:id/theaters -- belongs in the Movies controller.

But that has nothing to do with REST. The whole point of REST is that a resource identified by a specific URL can be located anywhere.

5
  • 1
    Personally, I disagree. I think the hierarchy of the URL doesn't matter to the controller. It's just a mapping. I prefer to determine based on what resource gets returned. So if the resource being returned is a Movie then you can find the code in the MovieController regardless of where in URL it appears. Feb 11 '15 at 4:42
  • 1
    That works too. The important thing is to be consistent enough so that the next programmer can follow your work. Which controller does an Invoice ViewModel go into? Feb 11 '15 at 4:55
  • Is that a trick question? I'll say, umm, the InvoiceController? Feb 11 '15 at 5:38
  • So you want one controller for every output artifact, then. That works, I guess. But perhaps a Reports controller makes just as much sense. Feb 11 '15 at 20:14
  • I see what you're saying. That sounds perfectly reasonable. Feb 11 '15 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.