/** * Loads the given view using the layout and parameters in $this. * @param string $view The view to load. This should be a full view. It will not be appended to * the viewDir */ function loadView($view, $params = null) { if (!in_array($this->outputFormat, $this->allowedFormats)) { throw new RuntimeException("The extension '{$this->outputFormat}' is not supported."); } if (func_num_args() == 1) { $params = $this; } switch ($this->outputFormat) { case 'html': if ($this->layout && $this->renderPartial === false) { $this['content_view'] = $view; $view = $this->layout; } View::load($view, $params, false); break; case 'json': if (!headers_sent()) { header('Content-type: application/json; charset=utf-8'); if ($this->isRestful() && !empty($this['errors'])) { header($_SERVER['SERVER_PROTOCOL'] . ' 400 Error'); $params = array('errors' => $this['errors']); } } echo json_encode_all($params); break; case 'jsonp': if (!headers_sent()) { header('Content-type: application/javascript; charset=utf-8'); if ($this->isRestful() && !empty($this['errors'])) { header($_SERVER['SERVER_PROTOCOL'] . ' 400 Error'); $params = array('errors' => $this['errors']); } } $callback = 'callback'; if ($this->route) { $callback = $this->route->getJsonPCallback(); } echo $callback . '(' . json_encode_all($params) . ')'; break; case 'xml': if (!headers_sent()) { header('Content-type: application/xml'); if ($this->isRestful() && !empty($this['errors'])) { header($_SERVER['SERVER_PROTOCOL'] . ' 400 Error'); $params = array('errors' => $this['errors']); } } echo xml_encode_all($params); break; default: throw new RuntimeException("The extension '{$this->outputFormat}' is not supported."); break; } $this->loadView = false; }
public function testSetRequestParamsDetectsJsonPCallback() { $this->route->setRequestParams(array('callback' => 'myCallback')); $this->assertEquals('jsonp', $this->route->getExtension()); $this->assertEquals('myCallback', $this->route->getJsonPCallback()); }