Support the ongoing development of Laravel.io →
Requests Views

Having a resource controller, there are the default representations of the used models as a website (HTML served by access via browser). But I would also like to manage the resources behind the controller over a REST API without repeating code just to serve JSON instead of HTML.

How would you solve this? My ideas so far:

  • Detect in each controller action if a REST API specific parameter is set to switch output format (seems to me as the least sane idea).
  • Define different routes, in example an "api" subdomain or "api" prefix for the same routes to hand over the information the controllers should print JSON instead of HTML (would still contain the ifs in the controller actions).
  • Implement independent controllers for REST API in own namespace (contradictionary to DRY pattern, partially, since the "HTML-controllers" only provide reading access to resources)

Access to REST API should be restricted by OAuth. Would it still be reasonable to cram content delivery and REST API into the same controller?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

alomvar alomvar Joined 5 Aug 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.