gb_admin::json_rsp PHP Метод

json_rsp() статический публичный Метод

static public json_rsp ( $data = null, $status = '200 OK', $exit = true, $pretty = true )
    static function json_rsp($data = null, $status = '200 OK', $exit = true, $pretty = true)
    {
        if ($data !== null) {
            $data = $pretty ? json::pretty($data) . "\n" : json_encode($data);
        } else {
            $data = '';
        }
        self::abrupt_rsp($data, $status, 'application/json; charset=utf-8', $exit);
    }

Usage Example

Пример #1
0
        foreach ($state_fields as $k => $discard) {
            if ($k === 'body') {
                $modified_state[$k] = $post->rawBody();
            } else {
                $v = $post->{$k};
                if ($v instanceof GBDateTime) {
                    $v = strval($v);
                }
                $modified_state[$k] = $v;
            }
        }
    }
    # commit?
    if ($input['commit']) {
        git::add($post->name);
        git::commit(($created ? 'Created' : 'Updated') . ' post ' . r($post->title), gb::$authorized, $post->name);
    }
    # build response entity
    $rsp = array('name' => $post->name, 'version' => $post->id, 'exists' => $post->exists(), 'isTracked' => $post->isTracked(), 'isDirty' => $post->isDirty(), 'state' => $modified_state);
    # status
    $status = '200 OK';
    if ($created) {
        $status = '201 Created';
    }
    # send JSON response
    gb_admin::json_rsp($rsp, $status);
    gb::log('saved post %s', $post->name);
} catch (Exception $e) {
    gb::log('failed to save post: %s', GBException::format($e, true, false, null, 0));
    gb_admin::json_rsp($e->getMessage(), '400 Bad Request');
}