parent
2f2b898a01
commit
1815ef1521
|
@ -32,13 +32,14 @@ abstract class RequestHandler
|
|||
$this->processApiRequest();
|
||||
}
|
||||
|
||||
protected function sendJson($object)
|
||||
public function sendJson($object)
|
||||
{
|
||||
$this->response->setHeader('Content-Type', 'application/json', true);
|
||||
$this->response->sendHeaders();
|
||||
echo json_encode($object, JSON_PRETTY_PRINT) . "\n";
|
||||
}
|
||||
|
||||
protected function sendJsonError($error, $code = null)
|
||||
public function sendJsonError($error, $code = null)
|
||||
{
|
||||
$response = $this->response;
|
||||
if ($code === null) {
|
||||
|
@ -55,6 +56,7 @@ abstract class RequestHandler
|
|||
$message = $error;
|
||||
}
|
||||
|
||||
$response->sendHeaders();
|
||||
$this->sendJson((object) ['error' => $message]);
|
||||
}
|
||||
|
||||
|
|
|
@ -42,15 +42,24 @@ abstract class ObjectController extends ActionController
|
|||
{
|
||||
parent::init();
|
||||
|
||||
$this->eventuallyLoadObject();
|
||||
if ($this->getRequest()->isApiRequest()) {
|
||||
$handler = new IcingaObjectHandler($this->getRequest(), $this->getResponse(), $this->db());
|
||||
try {
|
||||
$this->eventuallyLoadObject();
|
||||
} catch (NotFoundError $e) {
|
||||
// Silently ignore the error, the handler will complain
|
||||
$handler->sendJsonError($e, 404);
|
||||
// TODO: nice shutdown
|
||||
exit;
|
||||
}
|
||||
|
||||
$handler->setApi($this->api());
|
||||
if ($this->object) {
|
||||
$handler->setObject($this->object);
|
||||
}
|
||||
$handler->dispatch();
|
||||
} else {
|
||||
$this->eventuallyLoadObject();
|
||||
if ($this->getRequest()->getActionName() === 'add') {
|
||||
$this->addSingleTab(
|
||||
sprintf($this->translate('Add %s'), ucfirst($this->getType())),
|
||||
|
|
Loading…
Reference in New Issue