RequestHandlerComponent.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. <?php
  2. /**
  3. * Request object for handling alternative HTTP requests
  4. *
  5. * Alternative HTTP requests can come from wireless units like mobile phones, palmtop computers,
  6. * and the like. These units have no use for Ajax requests, and this Component can tell how Cake
  7. * should respond to the different needs of a handheld computer and a desktop machine.
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.Controller.Component
  18. * @since CakePHP(tm) v 0.10.4.1076
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Component', 'Controller');
  22. App::uses('Xml', 'Utility');
  23. /**
  24. * Request object for handling alternative HTTP requests
  25. *
  26. * Alternative HTTP requests can come from wireless units like mobile phones, palmtop computers,
  27. * and the like. These units have no use for Ajax requests, and this Component can tell how Cake
  28. * should respond to the different needs of a handheld computer and a desktop machine.
  29. *
  30. * @package Cake.Controller.Component
  31. * @link http://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html
  32. *
  33. */
  34. class RequestHandlerComponent extends Component {
  35. /**
  36. * The layout that will be switched to for Ajax requests
  37. *
  38. * @var string
  39. * @see RequestHandler::setAjax()
  40. */
  41. public $ajaxLayout = 'ajax';
  42. /**
  43. * Determines whether or not callbacks will be fired on this component
  44. *
  45. * @var boolean
  46. */
  47. public $enabled = true;
  48. /**
  49. * Holds the reference to Controller::$request
  50. *
  51. * @var CakeRequest
  52. */
  53. public $request;
  54. /**
  55. * Holds the reference to Controller::$response
  56. *
  57. * @var CakeResponse
  58. */
  59. public $response;
  60. /**
  61. * Contains the file extension parsed out by the Router
  62. *
  63. * @var string
  64. * @see Router::parseExtensions()
  65. */
  66. public $ext = null;
  67. /**
  68. * The template to use when rendering the given content type.
  69. *
  70. * @var string
  71. */
  72. protected $_renderType = null;
  73. /**
  74. * A mapping between extensions and deserializers for request bodies of that type.
  75. * By default only JSON and XML are mapped, use RequestHandlerComponent::addInputType()
  76. *
  77. * @var array
  78. */
  79. protected $_inputTypeMap = array(
  80. 'json' => array('json_decode', true)
  81. );
  82. /**
  83. * Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
  84. *
  85. * @param ComponentCollection $collection ComponentCollection object.
  86. * @param array $settings Array of settings.
  87. */
  88. public function __construct(ComponentCollection $collection, $settings = array()) {
  89. $default = array('checkHttpCache' => true);
  90. parent::__construct($collection, $settings + $default);
  91. $this->addInputType('xml', array(array($this, 'convertXml')));
  92. $Controller = $collection->getController();
  93. $this->request = $Controller->request;
  94. $this->response = $Controller->response;
  95. }
  96. /**
  97. * Checks to see if a file extension has been parsed by the Router, or if the
  98. * HTTP_ACCEPT_TYPE has matches only one content type with the supported extensions.
  99. * If there is only one matching type between the supported content types & extensions,
  100. * and the requested mime-types, RequestHandler::$ext is set to that value.
  101. *
  102. * @param Controller $controller A reference to the controller
  103. * @param array $settings Array of settings to _set().
  104. * @return void
  105. * @see Router::parseExtensions()
  106. */
  107. public function initialize(Controller $controller, $settings = array()) {
  108. if (isset($this->request->params['ext'])) {
  109. $this->ext = $this->request->params['ext'];
  110. }
  111. if (empty($this->ext) || $this->ext == 'html') {
  112. $this->_setExtension();
  113. }
  114. $this->params = $controller->params;
  115. $this->_set($settings);
  116. }
  117. /**
  118. * Set the extension based on the accept headers.
  119. * Compares the accepted types and configured extensions.
  120. * If there is one common type, that is assigned as the ext/content type
  121. * for the response.
  122. *
  123. * If html is one of the preferred types, no content type will be set, this
  124. * is to avoid issues with browsers that prefer html and several other content types.
  125. *
  126. * @return void
  127. */
  128. protected function _setExtension() {
  129. $accept = $this->request->parseAccept();
  130. if (empty($accept)) {
  131. return;
  132. }
  133. $extensions = Router::extensions();
  134. $preferred = array_shift($accept);
  135. $preferredTypes = $this->response->mapType($preferred);
  136. $similarTypes = array_intersect($extensions, $preferredTypes);
  137. if (count($similarTypes) === 1 && !in_array('xhtml', $preferredTypes) && !in_array('html', $preferredTypes)) {
  138. $this->ext = array_shift($similarTypes);
  139. }
  140. }
  141. /**
  142. * The startup method of the RequestHandler enables several automatic behaviors
  143. * related to the detection of certain properties of the HTTP request, including:
  144. *
  145. * - Disabling layout rendering for Ajax requests (based on the HTTP_X_REQUESTED_WITH header)
  146. * - If Router::parseExtensions() is enabled, the layout and template type are
  147. * switched based on the parsed extension or Accept-Type header. For example, if `controller/action.xml`
  148. * is requested, the view path becomes `app/View/Controller/xml/action.ctp`. Also if
  149. * `controller/action` is requested with `Accept-Type: application/xml` in the headers
  150. * the view path will become `app/View/Controller/xml/action.ctp`. Layout and template
  151. * types will only switch to mime-types recognized by CakeResponse. If you need to declare
  152. * additional mime-types, you can do so using CakeResponse::type() in your controllers beforeFilter()
  153. * method.
  154. * - If a helper with the same name as the extension exists, it is added to the controller.
  155. * - If the extension is of a type that RequestHandler understands, it will set that
  156. * Content-type in the response header.
  157. * - If the XML data is POSTed, the data is parsed into an XML object, which is assigned
  158. * to the $data property of the controller, which can then be saved to a model object.
  159. *
  160. * @param Controller $controller A reference to the controller
  161. * @return void
  162. */
  163. public function startup(Controller $controller) {
  164. $controller->request->params['isAjax'] = $this->request->is('ajax');
  165. $isRecognized = (
  166. !in_array($this->ext, array('html', 'htm')) &&
  167. $this->response->getMimeType($this->ext)
  168. );
  169. if (!empty($this->ext) && $isRecognized) {
  170. $this->renderAs($controller, $this->ext);
  171. } elseif ($this->request->is('ajax')) {
  172. $this->renderAs($controller, 'ajax');
  173. } elseif (empty($this->ext) || in_array($this->ext, array('html', 'htm'))) {
  174. $this->respondAs('html', array('charset' => Configure::read('App.encoding')));
  175. }
  176. foreach ($this->_inputTypeMap as $type => $handler) {
  177. if ($this->requestedWith($type)) {
  178. $input = call_user_func_array(array($controller->request, 'input'), $handler);
  179. $controller->request->data = $input;
  180. }
  181. }
  182. }
  183. /**
  184. * Helper method to parse xml input data, due to lack of anonymous functions
  185. * this lives here.
  186. *
  187. * @param string $xml
  188. * @return array Xml array data
  189. */
  190. public function convertXml($xml) {
  191. try {
  192. $xml = Xml::build($xml);
  193. if (isset($xml->data)) {
  194. return Xml::toArray($xml->data);
  195. }
  196. return Xml::toArray($xml);
  197. } catch (XmlException $e) {
  198. return array();
  199. }
  200. }
  201. /**
  202. * Handles (fakes) redirects for Ajax requests using requestAction()
  203. *
  204. * @param Controller $controller A reference to the controller
  205. * @param string|array $url A string or array containing the redirect location
  206. * @param integer|array $status HTTP Status for redirect
  207. * @param boolean $exit
  208. * @return void
  209. */
  210. public function beforeRedirect(Controller $controller, $url, $status = null, $exit = true) {
  211. if (!$this->request->is('ajax')) {
  212. return;
  213. }
  214. foreach ($_POST as $key => $val) {
  215. unset($_POST[$key]);
  216. }
  217. if (is_array($url)) {
  218. $url = Router::url($url + array('base' => false));
  219. }
  220. if (!empty($status)) {
  221. $statusCode = $this->response->httpCodes($status);
  222. $code = key($statusCode);
  223. $this->response->statusCode($code);
  224. }
  225. $this->response->body($this->requestAction($url, array('return', 'bare' => false)));
  226. $this->response->send();
  227. $this->_stop();
  228. }
  229. /**
  230. * Checks if the response can be considered different according to the request
  231. * headers, and the caching response headers. If it was not modified, then the
  232. * render process is skipped. And the client will get a blank response with a
  233. * "304 Not Modified" header.
  234. *
  235. * @params Controller $controller
  236. * @return boolean false if the render process should be aborted
  237. **/
  238. public function beforeRender(Controller $controller) {
  239. $shouldCheck = $this->settings['checkHttpCache'];
  240. if ($shouldCheck && $this->response->checkNotModified($this->request)) {
  241. return false;
  242. }
  243. }
  244. /**
  245. * Returns true if the current HTTP request is Ajax, false otherwise
  246. *
  247. * @return boolean True if call is Ajax
  248. * @deprecated use `$this->request->is('ajax')` instead.
  249. */
  250. public function isAjax() {
  251. return $this->request->is('ajax');
  252. }
  253. /**
  254. * Returns true if the current HTTP request is coming from a Flash-based client
  255. *
  256. * @return boolean True if call is from Flash
  257. * @deprecated use `$this->request->is('flash')` instead.
  258. */
  259. public function isFlash() {
  260. return $this->request->is('flash');
  261. }
  262. /**
  263. * Returns true if the current request is over HTTPS, false otherwise.
  264. *
  265. * @return boolean True if call is over HTTPS
  266. * @deprecated use `$this->request->is('ssl')` instead.
  267. */
  268. public function isSSL() {
  269. return $this->request->is('ssl');
  270. }
  271. /**
  272. * Returns true if the current call accepts an XML response, false otherwise
  273. *
  274. * @return boolean True if client accepts an XML response
  275. */
  276. public function isXml() {
  277. return $this->prefers('xml');
  278. }
  279. /**
  280. * Returns true if the current call accepts an RSS response, false otherwise
  281. *
  282. * @return boolean True if client accepts an RSS response
  283. */
  284. public function isRss() {
  285. return $this->prefers('rss');
  286. }
  287. /**
  288. * Returns true if the current call accepts an Atom response, false otherwise
  289. *
  290. * @return boolean True if client accepts an RSS response
  291. */
  292. public function isAtom() {
  293. return $this->prefers('atom');
  294. }
  295. /**
  296. * Returns true if user agent string matches a mobile web browser, or if the
  297. * client accepts WAP content.
  298. *
  299. * @return boolean True if user agent is a mobile web browser
  300. */
  301. public function isMobile() {
  302. return $this->request->is('mobile') || $this->accepts('wap');
  303. }
  304. /**
  305. * Returns true if the client accepts WAP content
  306. *
  307. * @return boolean
  308. */
  309. public function isWap() {
  310. return $this->prefers('wap');
  311. }
  312. /**
  313. * Returns true if the current call a POST request
  314. *
  315. * @return boolean True if call is a POST
  316. * @deprecated Use $this->request->is('post'); from your controller.
  317. */
  318. public function isPost() {
  319. return $this->request->is('post');
  320. }
  321. /**
  322. * Returns true if the current call a PUT request
  323. *
  324. * @return boolean True if call is a PUT
  325. * @deprecated Use $this->request->is('put'); from your controller.
  326. */
  327. public function isPut() {
  328. return $this->request->is('put');
  329. }
  330. /**
  331. * Returns true if the current call a GET request
  332. *
  333. * @return boolean True if call is a GET
  334. * @deprecated Use $this->request->is('get'); from your controller.
  335. */
  336. public function isGet() {
  337. return $this->request->is('get');
  338. }
  339. /**
  340. * Returns true if the current call a DELETE request
  341. *
  342. * @return boolean True if call is a DELETE
  343. * @deprecated Use $this->request->is('delete'); from your controller.
  344. */
  345. public function isDelete() {
  346. return $this->request->is('delete');
  347. }
  348. /**
  349. * Gets Prototype version if call is Ajax, otherwise empty string.
  350. * The Prototype library sets a special "Prototype version" HTTP header.
  351. *
  352. * @return string Prototype version of component making Ajax call
  353. */
  354. public function getAjaxVersion() {
  355. if (env('HTTP_X_PROTOTYPE_VERSION') != null) {
  356. return env('HTTP_X_PROTOTYPE_VERSION');
  357. }
  358. return false;
  359. }
  360. /**
  361. * Adds/sets the Content-type(s) for the given name. This method allows
  362. * content-types to be mapped to friendly aliases (or extensions), which allows
  363. * RequestHandler to automatically respond to requests of that type in the
  364. * startup method.
  365. *
  366. * @param string $name The name of the Content-type, i.e. "html", "xml", "css"
  367. * @param string|array $type The Content-type or array of Content-types assigned to the name,
  368. * i.e. "text/html", or "application/xml"
  369. * @return void
  370. * @deprecated use `$this->response->type()` instead.
  371. */
  372. public function setContent($name, $type = null) {
  373. $this->response->type(array($name => $type));
  374. }
  375. /**
  376. * Gets the server name from which this request was referred
  377. *
  378. * @return string Server address
  379. * @deprecated use $this->request->referer() from your controller instead
  380. */
  381. public function getReferer() {
  382. return $this->request->referer(false);
  383. }
  384. /**
  385. * Gets remote client IP
  386. *
  387. * @param boolean $safe
  388. * @return string Client IP address
  389. * @deprecated use $this->request->clientIp() from your, controller instead.
  390. */
  391. public function getClientIP($safe = true) {
  392. return $this->request->clientIp($safe);
  393. }
  394. /**
  395. * Determines which content types the client accepts. Acceptance is based on
  396. * the file extension parsed by the Router (if present), and by the HTTP_ACCEPT
  397. * header. Unlike CakeRequest::accepts() this method deals entirely with mapped content types.
  398. *
  399. * Usage:
  400. *
  401. * `$this->RequestHandler->accepts(array('xml', 'html', 'json'));`
  402. *
  403. * Returns true if the client accepts any of the supplied types.
  404. *
  405. * `$this->RequestHandler->accepts('xml');`
  406. *
  407. * Returns true if the client accepts xml.
  408. *
  409. * @param string|array $type Can be null (or no parameter), a string type name, or an
  410. * array of types
  411. * @return mixed If null or no parameter is passed, returns an array of content
  412. * types the client accepts. If a string is passed, returns true
  413. * if the client accepts it. If an array is passed, returns true
  414. * if the client accepts one or more elements in the array.
  415. * @see RequestHandlerComponent::setContent()
  416. */
  417. public function accepts($type = null) {
  418. $accepted = $this->request->accepts();
  419. if ($type == null) {
  420. return $this->mapType($accepted);
  421. } elseif (is_array($type)) {
  422. foreach ($type as $t) {
  423. $t = $this->mapAlias($t);
  424. if (in_array($t, $accepted)) {
  425. return true;
  426. }
  427. }
  428. return false;
  429. } elseif (is_string($type)) {
  430. $type = $this->mapAlias($type);
  431. return in_array($type, $accepted);
  432. }
  433. return false;
  434. }
  435. /**
  436. * Determines the content type of the data the client has sent (i.e. in a POST request)
  437. *
  438. * @param string|array $type Can be null (or no parameter), a string type name, or an array of types
  439. * @return mixed If a single type is supplied a boolean will be returned. If no type is provided
  440. * The mapped value of CONTENT_TYPE will be returned. If an array is supplied the first type
  441. * in the request content type will be returned.
  442. */
  443. public function requestedWith($type = null) {
  444. if (!$this->request->is('post') && !$this->request->is('put')) {
  445. return null;
  446. }
  447. list($contentType) = explode(';', env('CONTENT_TYPE'));
  448. if ($type == null) {
  449. return $this->mapType($contentType);
  450. } elseif (is_array($type)) {
  451. foreach ($type as $t) {
  452. if ($this->requestedWith($t)) {
  453. return $t;
  454. }
  455. }
  456. return false;
  457. } elseif (is_string($type)) {
  458. return ($type == $this->mapType($contentType));
  459. }
  460. }
  461. /**
  462. * Determines which content-types the client prefers. If no parameters are given,
  463. * the single content-type that the client most likely prefers is returned. If $type is
  464. * an array, the first item in the array that the client accepts is returned.
  465. * Preference is determined primarily by the file extension parsed by the Router
  466. * if provided, and secondarily by the list of content-types provided in
  467. * HTTP_ACCEPT.
  468. *
  469. * @param string|array $type An optional array of 'friendly' content-type names, i.e.
  470. * 'html', 'xml', 'js', etc.
  471. * @return mixed If $type is null or not provided, the first content-type in the
  472. * list, based on preference, is returned. If a single type is provided
  473. * a boolean will be returned if that type is preferred.
  474. * If an array of types are provided then the first preferred type is returned.
  475. * If no type is provided the first preferred type is returned.
  476. * @see RequestHandlerComponent::setContent()
  477. */
  478. public function prefers($type = null) {
  479. $acceptRaw = $this->request->parseAccept();
  480. if (empty($acceptRaw)) {
  481. return $this->ext;
  482. }
  483. $accepts = array_shift($acceptRaw);
  484. $accepts = $this->mapType($accepts);
  485. if ($type == null) {
  486. if (empty($this->ext) && !empty($accepts)) {
  487. return $accepts[0];
  488. }
  489. return $this->ext;
  490. }
  491. $types = (array)$type;
  492. if (count($types) === 1) {
  493. if (!empty($this->ext)) {
  494. return in_array($this->ext, $types);
  495. }
  496. return in_array($types[0], $accepts);
  497. }
  498. $intersect = array_values(array_intersect($accepts, $types));
  499. if (empty($intersect)) {
  500. return false;
  501. }
  502. return $intersect[0];
  503. }
  504. /**
  505. * Sets the layout and template paths for the content type defined by $type.
  506. *
  507. * ### Usage:
  508. *
  509. * Render the response as an 'ajax' response.
  510. *
  511. * `$this->RequestHandler->renderAs($this, 'ajax');`
  512. *
  513. * Render the response as an xml file and force the result as a file download.
  514. *
  515. * `$this->RequestHandler->renderAs($this, 'xml', array('attachment' => 'myfile.xml');`
  516. *
  517. * @param Controller $controller A reference to a controller object
  518. * @param string $type Type of response to send (e.g: 'ajax')
  519. * @param array $options Array of options to use
  520. * @return void
  521. * @see RequestHandlerComponent::setContent()
  522. * @see RequestHandlerComponent::respondAs()
  523. */
  524. public function renderAs(Controller $controller, $type, $options = array()) {
  525. $defaults = array('charset' => 'UTF-8');
  526. if (Configure::read('App.encoding') !== null) {
  527. $defaults['charset'] = Configure::read('App.encoding');
  528. }
  529. $options = array_merge($defaults, $options);
  530. if ($type == 'ajax') {
  531. $controller->layout = $this->ajaxLayout;
  532. return $this->respondAs('html', $options);
  533. }
  534. $controller->ext = '.ctp';
  535. $viewClass = Inflector::classify($type);
  536. $viewName = $viewClass . 'View';
  537. if (!class_exists($viewName)) {
  538. App::uses($viewName, 'View');
  539. }
  540. if (class_exists($viewName)) {
  541. $controller->viewClass = $viewClass;
  542. } elseif (empty($this->_renderType)) {
  543. $controller->viewPath .= DS . $type;
  544. } else {
  545. $remove = preg_replace("/([\/\\\\]{$this->_renderType})$/", DS . $type, $controller->viewPath);
  546. $controller->viewPath = $remove;
  547. }
  548. $this->_renderType = $type;
  549. $controller->layoutPath = $type;
  550. if ($this->response->getMimeType($type)) {
  551. $this->respondAs($type, $options);
  552. }
  553. $helper = ucfirst($type);
  554. $isAdded = (
  555. in_array($helper, $controller->helpers) ||
  556. array_key_exists($helper, $controller->helpers)
  557. );
  558. if (!$isAdded) {
  559. App::uses('AppHelper', 'View/Helper');
  560. App::uses($helper . 'Helper', 'View/Helper');
  561. if (class_exists($helper . 'Helper')) {
  562. $controller->helpers[] = $helper;
  563. }
  564. }
  565. }
  566. /**
  567. * Sets the response header based on type map index name. This wraps several methods
  568. * available on CakeResponse. It also allows you to use Content-Type aliases.
  569. *
  570. * @param string|array $type Friendly type name, i.e. 'html' or 'xml', or a full content-type,
  571. * like 'application/x-shockwave'.
  572. * @param array $options If $type is a friendly type name that is associated with
  573. * more than one type of content, $index is used to select which content-type to use.
  574. * @return boolean Returns false if the friendly type name given in $type does
  575. * not exist in the type map, or if the Content-type header has
  576. * already been set by this method.
  577. * @see RequestHandlerComponent::setContent()
  578. */
  579. public function respondAs($type, $options = array()) {
  580. $defaults = array('index' => null, 'charset' => null, 'attachment' => false);
  581. $options = $options + $defaults;
  582. if (strpos($type, '/') === false) {
  583. $cType = $this->response->getMimeType($type);
  584. if ($cType === false) {
  585. return false;
  586. }
  587. if (is_array($cType) && isset($cType[$options['index']])) {
  588. $cType = $cType[$options['index']];
  589. }
  590. if (is_array($cType)) {
  591. if ($this->prefers($cType)) {
  592. $cType = $this->prefers($cType);
  593. } else {
  594. $cType = $cType[0];
  595. }
  596. }
  597. } else {
  598. $cType = $type;
  599. }
  600. if ($cType != null) {
  601. if (empty($this->request->params['requested'])) {
  602. $this->response->type($cType);
  603. }
  604. if (!empty($options['charset'])) {
  605. $this->response->charset($options['charset']);
  606. }
  607. if (!empty($options['attachment'])) {
  608. $this->response->download($options['attachment']);
  609. }
  610. return true;
  611. }
  612. return false;
  613. }
  614. /**
  615. * Returns the current response type (Content-type header), or null if not alias exists
  616. *
  617. * @return mixed A string content type alias, or raw content type if no alias map exists,
  618. * otherwise null
  619. */
  620. public function responseType() {
  621. return $this->mapType($this->response->type());
  622. }
  623. /**
  624. * Maps a content-type back to an alias
  625. *
  626. * @param string|array $cType Either a string content type to map, or an array of types.
  627. * @return string|array Aliases for the types provided.
  628. * @deprecated Use $this->response->mapType() in your controller instead.
  629. */
  630. public function mapType($cType) {
  631. return $this->response->mapType($cType);
  632. }
  633. /**
  634. * Maps a content type alias back to its mime-type(s)
  635. *
  636. * @param string|array $alias String alias to convert back into a content type. Or an array of aliases to map.
  637. * @return string Null on an undefined alias. String value of the mapped alias type. If an
  638. * alias maps to more than one content type, the first one will be returned.
  639. */
  640. public function mapAlias($alias) {
  641. if (is_array($alias)) {
  642. return array_map(array($this, 'mapAlias'), $alias);
  643. }
  644. $type = $this->response->getMimeType($alias);
  645. if ($type) {
  646. if (is_array($type)) {
  647. return $type[0];
  648. }
  649. return $type;
  650. }
  651. return null;
  652. }
  653. /**
  654. * Add a new mapped input type. Mapped input types are automatically
  655. * converted by RequestHandlerComponent during the startup() callback.
  656. *
  657. * @param string $type The type alias being converted, ie. json
  658. * @param array $handler The handler array for the type. The first index should
  659. * be the handling callback, all other arguments should be additional parameters
  660. * for the handler.
  661. * @return void
  662. * @throws CakeException
  663. */
  664. public function addInputType($type, $handler) {
  665. if (!is_array($handler) || !isset($handler[0]) || !is_callable($handler[0])) {
  666. throw new CakeException(__d('cake_dev', 'You must give a handler callback.'));
  667. }
  668. $this->_inputTypeMap[$type] = $handler;
  669. }
  670. }