mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
3303a2cda1
Conflicts: lib/Cake/Console/Templates/skel/Config/Schema/db_acl.php lib/Cake/Console/Templates/skel/Config/Schema/i18n.php lib/Cake/Console/Templates/skel/Config/Schema/sessions.php lib/Cake/Console/Templates/skel/Config/acl.ini.php lib/Cake/Console/Templates/skel/Config/acl.php lib/Cake/Console/Templates/skel/Config/bootstrap.php lib/Cake/Console/Templates/skel/Config/core.php lib/Cake/Console/Templates/skel/Config/database.php.default lib/Cake/Console/Templates/skel/Config/email.php.default lib/Cake/Console/Templates/skel/Config/routes.php lib/Cake/Console/Templates/skel/Console/Command/AppShell.php lib/Cake/Console/Templates/skel/Console/cake.bat lib/Cake/Console/Templates/skel/Console/cake.php lib/Cake/Console/Templates/skel/Controller/AppController.php lib/Cake/Console/Templates/skel/Controller/PagesController.php lib/Cake/Console/Templates/skel/Model/AppModel.php lib/Cake/Console/Templates/skel/View/Errors/error400.ctp lib/Cake/Console/Templates/skel/View/Errors/error500.ctp lib/Cake/Console/Templates/skel/View/Helper/AppHelper.php lib/Cake/Console/Templates/skel/View/Layouts/Emails/html/default.ctp lib/Cake/Console/Templates/skel/View/Layouts/ajax.ctp lib/Cake/Console/Templates/skel/View/Layouts/default.ctp lib/Cake/Console/Templates/skel/View/Layouts/error.ctp lib/Cake/Console/Templates/skel/View/Layouts/flash.ctp lib/Cake/Console/Templates/skel/View/Pages/home.ctp lib/Cake/Console/Templates/skel/index.php lib/Cake/Console/Templates/skel/webroot/index.php lib/Cake/Console/Templates/skel/webroot/test.php
131 lines
4.1 KiB
PHP
131 lines
4.1 KiB
PHP
<?php
|
|
/**
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
*
|
|
* Licensed under The MIT License
|
|
* For full copyright and license information, please see the LICENSE.txt
|
|
* Redistributions of files must retain the above copyright notice.
|
|
*
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
*/
|
|
|
|
App::uses('View', 'View');
|
|
|
|
/**
|
|
* A view class that is used for JSON responses.
|
|
*
|
|
* By setting the '_serialize' key in your controller, you can specify a view variable
|
|
* that should be serialized to JSON and used as the response for the request.
|
|
* This allows you to omit views + layouts, if your just need to emit a single view
|
|
* variable as the JSON response.
|
|
*
|
|
* In your controller, you could do the following:
|
|
*
|
|
* `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
|
|
*
|
|
* When the view is rendered, the `$posts` view variable will be serialized
|
|
* into JSON.
|
|
*
|
|
* You can also define `'_serialize'` as an array. This will create a top level object containing
|
|
* all the named view variables:
|
|
*
|
|
* {{{
|
|
* $this->set(compact('posts', 'users', 'stuff'));
|
|
* $this->set('_serialize', array('posts', 'users'));
|
|
* }}}
|
|
*
|
|
* The above would generate a JSON object that looks like:
|
|
*
|
|
* `{"posts": [...], "users": [...]}`
|
|
*
|
|
* If you don't use the `_serialize` key, you will need a view. 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 paramater name which will contain the callback function name.
|
|
*
|
|
* @package Cake.View
|
|
* @since CakePHP(tm) v 2.1.0
|
|
*/
|
|
class JsonView extends View {
|
|
|
|
/**
|
|
* JSON views are always located in the 'json' sub directory for a
|
|
* controllers views.
|
|
*
|
|
* @var string
|
|
*/
|
|
public $subDir = 'json';
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param Controller $controller
|
|
*/
|
|
public function __construct(Controller $controller = null) {
|
|
parent::__construct($controller);
|
|
if (isset($controller->response) && $controller->response instanceof CakeResponse) {
|
|
$controller->response->type('json');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Render a JSON view.
|
|
*
|
|
* ### Special parameters
|
|
* `_serialize` To convert a set of view variables into a JSON response.
|
|
* It's value can be a string for single variable name or array for multiple names.
|
|
* You can omit the`_serialize` parameter, and use a normal view + layout as well.
|
|
* `_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.
|
|
*
|
|
* @param string $view The view being rendered.
|
|
* @param string $layout The layout being rendered.
|
|
* @return string The rendered view.
|
|
*/
|
|
public function render($view = null, $layout = null) {
|
|
$return = null;
|
|
if (isset($this->viewVars['_serialize'])) {
|
|
$return = $this->_serialize($this->viewVars['_serialize']);
|
|
} elseif ($view !== false && $this->_getViewFileName($view)) {
|
|
$return = parent::render($view, false);
|
|
}
|
|
|
|
if (!empty($this->viewVars['_jsonp'])) {
|
|
$jsonpParam = $this->viewVars['_jsonp'];
|
|
if ($this->viewVars['_jsonp'] === true) {
|
|
$jsonpParam = 'callback';
|
|
}
|
|
if (isset($this->request->query[$jsonpParam])) {
|
|
$return = sprintf('%s(%s)', h($this->request->query[$jsonpParam]), $return);
|
|
$this->response->type('js');
|
|
}
|
|
}
|
|
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* Serialize view vars
|
|
*
|
|
* @param array $serialize The viewVars that need to be serialized
|
|
* @return string The serialized data
|
|
*/
|
|
protected function _serialize($serialize) {
|
|
if (is_array($serialize)) {
|
|
$data = array();
|
|
foreach ($serialize as $key) {
|
|
$data[$key] = $this->viewVars[$key];
|
|
}
|
|
} else {
|
|
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
|
|
}
|
|
return json_encode($data);
|
|
}
|
|
|
|
}
|