What I want is the ability to set any of my modules with a flag that will make all the controllers of that module act as REST controllers. So basically any return values from the controller will be json encoded and returned.
I currently have the following code in my RESTController.
public function afterExecuteRoute(Dispatcher $dispatcher) {
$data = $dispatcher->getReturnedValue();
if (!is_array($data)) {
$message = $data;
$data = [];
$data['message'] = $message;
}
$response = new Response;
$response->setJsonContent($data);
return $response;
}
But what I really would like to do is transfer this code to the Module level somehow. So if for example I have a module called "api". I would like to define it so that the api module always passes back data json encoded.
Also, my other problem with the above is that when doing an HTTP request, what I get back seems to be fine, but when doing tests through CLI, the response value seems to be doubled.