I have a Laravel application with worms and their respective RESTful resource controllers , for example:
Model
class Entity extends Eloquent {
...
}
Controller
class EntityContoller {
public function index() {
Entity $entities = Entity::all();
return view('entity.index', compact($entities));
}
...
}
Now I am creating an Android application, and instead of returning the views, I need the data as JSON.
In my current solution, for each request that I make from my Android application, I add a request request parameter contentType=JSON. I detect this in the controller and send the data accordingly as follows. But that seems tedious, and I have to write the same condition everywhere.
class EntityContoller {
public function index() {
Entity $entities = Entity::all();
if(Request::get('contentType', 'JSON')) return $entities;
return view('entity.index', compact($entities));
}
...
}
How can I do this without writing this condition to every controller action?