recursive_acronym
recursive_acronym

Reputation: 3031

Why keep api controllers separate from typical action controllers?

Some example rails/angularjs apps I have seen keep the API under its own namespace. These controllers only respond with json. I don't understand why these require their own namespace given that controller actions are intended to be able to respond to different types (html,js).

For example https://github.com/mkwiatkowski/todo-rails4-angularjs

Upvotes: 1

Views: 127

Answers (2)

lightswitch05
lightswitch05

Reputation: 9428

I keep my API separate because my controllers return HTML. I know it is easy to return different formats from the controller, but once you start getting a lot of different formats in the controller it quickly starts taking up a lot of space and subtracts from the actual controller logic.

Another reason, perhaps even more important, is versioning. With an API you generally have clients which may be out of your control. It's much easier to provide support for different versions of APIs when you have them namespaced outside of the standard controller. This way, if you have a breaking change, you are able to just create a new version and not worry about older clients which might have not been updated yet.

Upvotes: 2

seand
seand

Reputation: 5296

A common reason for keeping these separate is authentication. Typical UI controllers may use sessions, but REST/API controllers often don't use this. An api token is typical.

Upvotes: 1

Related Questions