|
|
@@ -82,15 +82,12 @@ class RequestHandlerComponent extends Component {
|
|
|
*
|
|
|
* - `checkHttpCache` - Whether to check for http cache.
|
|
|
* - `viewClassMap` - Mapping between type and view class.
|
|
|
- * - `ajaxLayout` - The layout that will be switched to for Ajax requests.
|
|
|
- * See RequestHandler::setAjax()
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
protected $_defaultConfig = [
|
|
|
'checkHttpCache' => true,
|
|
|
'viewClassMap' => '',
|
|
|
- 'ajaxLayout' => 'ajax'
|
|
|
];
|
|
|
|
|
|
/**
|
|
|
@@ -105,13 +102,14 @@ class RequestHandlerComponent extends Component {
|
|
|
|
|
|
/**
|
|
|
* A mapping between type and viewClass
|
|
|
- * By default only JSON and XML are mapped, use RequestHandlerComponent::viewClassMap()
|
|
|
+ * By default only JSON, XML, and AJAX are mapped. Use RequestHandlerComponent::viewClassMap().
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
protected $_viewClassMap = array(
|
|
|
'json' => 'Json',
|
|
|
- 'xml' => 'Xml'
|
|
|
+ 'xml' => 'Xml',
|
|
|
+ 'ajax' => 'Ajax'
|
|
|
);
|
|
|
|
|
|
/**
|
|
|
@@ -130,10 +128,10 @@ class RequestHandlerComponent extends Component {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Checks to see if a file extension has been parsed by the Router, or if the
|
|
|
- * HTTP_ACCEPT_TYPE has matches only one content type with the supported extensions.
|
|
|
- * If there is only one matching type between the supported content types & extensions,
|
|
|
- * and the requested mime-types, RequestHandler::$ext is set to that value.
|
|
|
+ * Checks to see if a specific content type has been requested and sets RequestHandler::$ext
|
|
|
+ * accordingly. Checks the following in order: 1. The '_ext' value parsed by the Router. 2. A specific
|
|
|
+ * AJAX type request indicated by the presence of a header. 3. The Accept header.
|
|
|
+ * With the exception of an ajax type request the type must have been configured in the Router.
|
|
|
*
|
|
|
* @param Event $event The initialize event that was fired.
|
|
|
* @return void
|
|
|
@@ -143,7 +141,10 @@ class RequestHandlerComponent extends Component {
|
|
|
if (isset($this->request->params['_ext'])) {
|
|
|
$this->ext = $this->request->params['_ext'];
|
|
|
}
|
|
|
- if (empty($this->ext) || $this->ext === 'html') {
|
|
|
+ if(empty($this->ext) && $this->request->is('ajax')) {
|
|
|
+ $this->ext = 'ajax';
|
|
|
+ }
|
|
|
+ if (empty($this->ext) || in_array($this->ext, array('html', 'htm'))) {
|
|
|
$this->_setExtension();
|
|
|
}
|
|
|
|
|
|
@@ -176,7 +177,7 @@ class RequestHandlerComponent extends Component {
|
|
|
$accepts = $this->response->mapType($accept);
|
|
|
$preferedTypes = current($accepts);
|
|
|
if (array_intersect($preferedTypes, array('html', 'xhtml'))) {
|
|
|
- return null;
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
$extensions = Router::extensions();
|
|
|
@@ -193,7 +194,6 @@ class RequestHandlerComponent extends Component {
|
|
|
* The startup method of the RequestHandler enables several automatic behaviors
|
|
|
* related to the detection of certain properties of the HTTP request, including:
|
|
|
*
|
|
|
- * - Disabling layout rendering for Ajax requests (based on the HTTP_X_REQUESTED_WITH header)
|
|
|
* - If Router::parseExtensions() is enabled, the layout and template type are
|
|
|
* switched based on the parsed extension or Accept-Type header. For example, if `controller/action.xml`
|
|
|
* is requested, the view path becomes `app/View/Controller/xml/action.ctp`. Also if
|
|
|
@@ -221,8 +221,6 @@ class RequestHandlerComponent extends Component {
|
|
|
|
|
|
if (!empty($this->ext) && $isRecognized) {
|
|
|
$this->renderAs($controller, $this->ext);
|
|
|
- } elseif ($this->request->is('ajax')) {
|
|
|
- $this->renderAs($controller, 'ajax');
|
|
|
} elseif (empty($this->ext) || in_array($this->ext, array('html', 'htm'))) {
|
|
|
$this->respondAs('html', array('charset' => Configure::read('App.encoding')));
|
|
|
}
|
|
|
@@ -481,7 +479,8 @@ class RequestHandlerComponent extends Component {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sets the layout and template paths for the content type defined by $type.
|
|
|
+ * Sets either the view class if one exists or the layout and template path of the view.
|
|
|
+ * The names of these are derived from the $type input parameter.
|
|
|
*
|
|
|
* ### Usage:
|
|
|
*
|
|
|
@@ -502,18 +501,14 @@ class RequestHandlerComponent extends Component {
|
|
|
*/
|
|
|
public function renderAs(Controller $controller, $type, array $options = array()) {
|
|
|
$defaults = array('charset' => 'UTF-8');
|
|
|
+ $view = null;
|
|
|
+ $viewClassMap = $this->viewClassMap();
|
|
|
|
|
|
if (Configure::read('App.encoding') !== null) {
|
|
|
$defaults['charset'] = Configure::read('App.encoding');
|
|
|
}
|
|
|
$options += $defaults;
|
|
|
|
|
|
- if ($type === 'ajax') {
|
|
|
- $controller->layout = $this->_config['ajaxLayout'];
|
|
|
- return $this->respondAs('html', $options);
|
|
|
- }
|
|
|
-
|
|
|
- $viewClassMap = $this->viewClassMap();
|
|
|
if (array_key_exists($type, $viewClassMap)) {
|
|
|
$view = $viewClassMap[$type];
|
|
|
} else {
|