After creating a resource controller PhotosController
for a website that also does AJAX calls to the API, it appears that the resource controller can be used on both the normal website and as the API.
This displays a HTML page for Photo
with id = 1
http://domain.com/photos/1
and Javascript uses the following which updates the Photo
resource and returns a JSON response
PUT http://domain.com/api/v1/photos/1
Question: Will we have 2 PhotoControllers
, one to handle API usage and one for non-API?
No. You can have two separate routes point to the same controller and action.
Then, in your controller methods, you can test whether the request is from Ajax or not.
I use a route group with prefix for API calls:
Then, in the controller, I use this condition: