Amin
Amin

Reputation: 682

how to implement laravel REST services to use with HTML and Mobile backend

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

Answers (1)

Hassan
Hassan

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

Related Questions