PHP Класс Cake\View\JsonView

It allows you to omit templates if you just need to emit JSON string as response. In your controller, you could do the following: $this->set(['posts' => $posts]); $this->set('_serialize', true); When the view is rendered, the $posts view variable will be serialized into JSON. You can also set multiple view variables for serialization. This will create a top level object containing all the named view variables: $this->set(compact('posts', 'users', 'stuff')); $this->set('_serialize', true); The above would generate a JSON object that looks like: {"posts": [...], "users": [...]} You can also set '_serialize' to a string or array to serialize only the specified view variables. If you don't use the _serialize, you will need a view template. You can use extended views to provide layout-like functionality. You can also enable JSONP support by setting parameter _jsonp to true or a string to specify custom query string parameter name which will contain the callback function name.
Наследование: extends Cake\View\SerializedView
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$layoutPath string JSON layouts are located in the json sub directory of Layouts/
$subDir string JSON views are located in the 'json' sub directory for controllers' views.

Защищенные свойства (Protected)

Свойство Тип Описание
$_responseType string Response type.
$_specialVars array List of special view vars.

Открытые методы

Метод Описание
render ( string | null $view = null, string | null $layout = null ) : string | null Render a JSON view.

Защищенные методы

Метод Описание
_dataToSerialize ( array | string | boolean $serialize = true ) : mixed Returns data to be serialized.
_serialize ( array | string | boolean $serialize ) : string | false Serialize view vars

Описание методов

_dataToSerialize() защищенный Метод

Returns data to be serialized.
protected _dataToSerialize ( array | string | boolean $serialize = true ) : mixed
$serialize array | string | boolean The name(s) of the view variable(s) that need(s) to be serialized. If true all available view variables will be used.
Результат mixed The data to serialize.

_serialize() защищенный Метод

### Special parameters _jsonOptions You can set custom options for json_encode() this way, e.g. JSON_HEX_TAG | JSON_HEX_APOS.
protected _serialize ( array | string | boolean $serialize ) : string | false
$serialize array | string | boolean The name(s) of the view variable(s) that need(s) to be serialized. If true all available view variables.
Результат string | false The serialized data, or boolean false if not serializable.

render() публичный Метод

### Special parameters _serialize To convert a set of view variables into a JSON response. Its value can be a string for single variable name or array for multiple names. If true all view variables will be serialized. It unset normal view template will be rendered. _jsonp Enables JSONP support and wraps response in callback function provided in query string. - Setting it to true enables the default query string parameter "callback". - Setting it to a string value, uses the provided query string parameter for finding the JSONP callback name.
public render ( string | null $view = null, string | null $layout = null ) : string | null
$view string | null The view being rendered.
$layout string | null The layout being rendered.
Результат string | null The rendered view.

Описание свойств

$_responseType защищенное свойство

Response type.
protected string $_responseType
Результат string

$_specialVars защищенное свойство

List of special view vars.
protected array $_specialVars
Результат array

$layoutPath публичное свойство

JSON layouts are located in the json sub directory of Layouts/
public string $layoutPath
Результат string

$subDir публичное свойство

JSON views are located in the 'json' sub directory for controllers' views.
public string $subDir
Результат string