Reputation: 682
I am building a laravel site to use with mobile backend, I have the controllers set up, I am wondering how can I use the same route to return both HTML and JSON, for instance if I do a :
Post request to /users/
I would like to create a User, but if form is submitted through HTML I would like to return an HTML page, if it was posted using mobile client I would like to return status in JSON.
Or for instance, I send a:
get request to /article/1
I would like to return HTML page for web browser, and JSON for mobile client.
Is this possible or I should create 2 different routes / controllers?
Upvotes: 0
Views: 262
Reputation: 626
Perhaps you could use something like the Laravel 4 user agent class to achieve that.
So, like so you said, if mobile, return JSON:
if(Agent::isMobile()) {
//return jsonResponse
} else {
//return the view
};
Upvotes: 2