getMimeType() public method

e.g getMimeType('pdf'); // returns 'application/pdf'
public getMimeType ( string $alias ) : mixed
$alias string the content type alias to map
return mixed String mapped mime type or false if $alias is not mapped
 /**
  * Checks if the response can be considered different according to the request
  * headers, and the caching response headers. If it was not modified, then the
  * render process is skipped. And the client will get a blank response with a
  * "304 Not Modified" header.
  *
  * - If Router::extensions() 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 `controller/action` is
  *   requested with `Accept-Type: application/xml` in the headers the view
  *   path will become `app/View/Controller/xml/action.ctp`. Layout and template
  *   types will only switch to mime-types recognized by Cake\Network\Response.
  *   If you need to declare additional mime-types, you can do so using
  *   Cake\Network\Response::type() in your controller's beforeFilter() method.
  * - If a helper with the same name as the extension exists, it is added to
  *   the controller.
  * - If the extension is of a type that RequestHandler understands, it will
  *   set that Content-type in the response header.
  *
  * @param Event $event The Controller.beforeRender event.
  * @return bool false if the render process should be aborted
  */
 public function beforeRender(Event $event)
 {
     $isRecognized = !in_array($this->ext, ['html', 'htm']) && $this->response->getMimeType($this->ext);
     if (!empty($this->ext) && $isRecognized) {
         $this->renderAs($event->subject(), $this->ext);
     } elseif (empty($this->ext) || in_array($this->ext, ['html', 'htm'])) {
         $this->respondAs('html', ['charset' => Configure::read('App.encoding')]);
     }
     if ($this->_config['checkHttpCache'] && $this->response->checkNotModified($this->request)) {
         return false;
     }
 }
 /**
  * The startup method of the RequestHandler enables several automatic behaviors
  * related to the detection of certain properties of the HTTP request, including:
  *
  * - If Router::extensions() 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
  *   `controller/action` is requested with `Accept-Type: application/xml` in the headers
  *   the view path will become `app/View/Controller/xml/action.ctp`. Layout and template
  *   types will only switch to mime-types recognized by Cake\Network\Response. If you need to declare
  *   additional mime-types, you can do so using Cake\Network\Response::type() in your controller's beforeFilter()
  *   method.
  * - If a helper with the same name as the extension exists, it is added to the controller.
  * - If the extension is of a type that RequestHandler understands, it will set that
  *   Content-type in the response header.
  * - If the XML data is POSTed, the data is parsed into an XML object, which is assigned
  *   to the $data property of the controller, which can then be saved to a model object.
  *
  * @param Event $event The startup event that was fired.
  * @return void
  */
 public function startup(Event $event)
 {
     $controller = $event->subject();
     $request = $controller->request;
     if (isset($request->params['_ext'])) {
         $this->ext = $request->params['_ext'];
     }
     if (empty($this->ext) || in_array($this->ext, ['html', 'htm'])) {
         $this->_setExtension($request, $this->response);
     }
     if (empty($this->ext) && $request->is('ajax')) {
         $this->ext = 'ajax';
     }
     $request->params['isAjax'] = $this->request->is('ajax');
     $isRecognized = !in_array($this->ext, ['html', 'htm']) && $this->response->getMimeType($this->ext);
     if (!empty($this->ext) && $isRecognized) {
         $this->renderAs($controller, $this->ext);
     } elseif (empty($this->ext) || in_array($this->ext, ['html', 'htm'])) {
         $this->respondAs('html', ['charset' => Configure::read('App.encoding')]);
     }
     foreach ($this->_inputTypeMap as $type => $handler) {
         if ($this->requestedWith($type)) {
             $input = call_user_func_array([$request, 'input'], $handler);
             $request->data = (array) $input;
         }
     }
 }
 /**
  * Asserts content type
  *
  * @param string $type The content-type to check for.
  * @param string $message The failure message that will be appended to the generated message.
  * @return void
  */
 public function assertContentType($type, $message = '')
 {
     if (!$this->_response) {
         $this->fail('No response set, cannot assert content-type. ' . $message);
     }
     $alias = $this->_response->getMimeType($type);
     if ($alias !== false) {
         $type = $alias;
     }
     $result = $this->_response->type();
     $this->assertEquals($type, $result, $message);
 }
Beispiel #4
0
 /**
  * Returns an audio/video element
  *
  * ### Usage
  *
  * Using an audio file:
  *
  * ```
  * echo $this->Html->media('audio.mp3', ['fullBase' => true]);
  * ```
  *
  * Outputs:
  *
  * `<video src="http://www.somehost.com/files/audio.mp3">Fallback text</video>`
  *
  * Using a video file:
  *
  * ```
  * echo $this->Html->media('video.mp4', ['text' => 'Fallback text']);
  * ```
  *
  * Outputs:
  *
  * `<video src="/files/video.mp4">Fallback text</video>`
  *
  * Using multiple video files:
  *
  * ```
  * echo $this->Html->media(
  *      ['video.mp4', ['src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'"]],
  *      ['tag' => 'video', 'autoplay']
  * );
  * ```
  *
  * Outputs:
  *
  * ```
  * <video autoplay="autoplay">
  *      <source src="/files/video.mp4" type="video/mp4"/>
  *      <source src="/files/video.ogv" type="video/ogv; codecs='theora, vorbis'"/>
  * </video>
  * ```
  *
  * ### Options
  *
  * - `tag` Type of media element to generate, either "audio" or "video".
  *  If tag is not provided it's guessed based on file's mime type.
  * - `text` Text to include inside the audio/video tag
  * - `pathPrefix` Path prefix to use for relative URLs, defaults to 'files/'
  * - `fullBase` If provided the src attribute will get a full address including domain name
  *
  * @param string|array $path Path to the video file, relative to the webroot/{$options['pathPrefix']} directory.
  *  Or an array where each item itself can be a path string or an associate array containing keys `src` and `type`
  * @param array $options Array of HTML attributes, and special options above.
  * @return string Generated media element
  */
 public function media($path, array $options = [])
 {
     $options += ['tag' => null, 'pathPrefix' => 'files/', 'text' => ''];
     if (!empty($options['tag'])) {
         $tag = $options['tag'];
     } else {
         $tag = null;
     }
     if (is_array($path)) {
         $sourceTags = '';
         foreach ($path as &$source) {
             if (is_string($source)) {
                 $source = ['src' => $source];
             }
             if (!isset($source['type'])) {
                 $ext = pathinfo($source['src'], PATHINFO_EXTENSION);
                 $source['type'] = $this->response->getMimeType($ext);
             }
             $source['src'] = $this->Url->assetUrl($source['src'], $options);
             $sourceTags .= $this->formatTemplate('tagselfclosing', ['tag' => 'source', 'attrs' => $this->templater()->formatAttributes($source)]);
         }
         unset($source);
         $options['text'] = $sourceTags . $options['text'];
         unset($options['fullBase']);
     } else {
         if (empty($path) && !empty($options['src'])) {
             $path = $options['src'];
         }
         $options['src'] = $this->Url->assetUrl($path, $options);
     }
     if ($tag === null) {
         if (is_array($path)) {
             $mimeType = $path[0]['type'];
         } else {
             $mimeType = $this->response->getMimeType(pathinfo($path, PATHINFO_EXTENSION));
         }
         if (preg_match('#^video/#', $mimeType)) {
             $tag = 'video';
         } else {
             $tag = 'audio';
         }
     }
     if (isset($options['poster'])) {
         $options['poster'] = $this->Url->assetUrl($options['poster'], ['pathPrefix' => Configure::read('App.imageBaseUrl')] + $options);
     }
     $text = $options['text'];
     $options = array_diff_key($options, ['tag' => null, 'fullBase' => null, 'pathPrefix' => null, 'text' => null]);
     return $this->tag($tag, $text, $options);
 }
 /**
  * The startup method of the RequestHandler enables several automatic behaviors
  * related to the detection of certain properties of the HTTP request, including:
  *
  * - If Router::extensions() 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
  *   `controller/action` is requested with `Accept-Type: application/xml` in the headers
  *   the view path will become `app/View/Controller/xml/action.ctp`. Layout and template
  *   types will only switch to mime-types recognized by Cake\Network\Response. If you need to declare
  *   additional mime-types, you can do so using Cake\Network\Response::type() in your controllers beforeFilter()
  *   method.
  * - If a helper with the same name as the extension exists, it is added to the controller.
  * - If the extension is of a type that RequestHandler understands, it will set that
  *   Content-type in the response header.
  * - If the XML data is POSTed, the data is parsed into an XML object, which is assigned
  *   to the $data property of the controller, which can then be saved to a model object.
  *
  * @param Event $event The startup event that was fired.
  * @return void
  */
 public function startup(Event $event)
 {
     $controller = $event->subject();
     $controller->request->params['isAjax'] = $this->request->is('ajax');
     $isRecognized = !in_array($this->ext, array('html', 'htm')) && $this->response->getMimeType($this->ext);
     if (!empty($this->ext) && $isRecognized) {
         $this->renderAs($controller, $this->ext);
     } elseif (empty($this->ext) || in_array($this->ext, array('html', 'htm'))) {
         $this->respondAs('html', array('charset' => Configure::read('App.encoding')));
     }
     foreach ($this->_inputTypeMap as $type => $handler) {
         if ($this->requestedWith($type)) {
             $input = call_user_func_array(array($controller->request, 'input'), $handler);
             $controller->request->data = $input;
         }
     }
 }
Beispiel #6
0
 /**
  * Tests the request object constructor
  *
  * @return void
  */
 public function testConstruct()
 {
     $response = new Response();
     $this->assertNull($response->body());
     $this->assertEquals('UTF-8', $response->charset());
     $this->assertEquals('text/html', $response->type());
     $this->assertEquals(200, $response->statusCode());
     $options = ['body' => 'This is the body', 'charset' => 'my-custom-charset', 'type' => 'mp3', 'status' => '203'];
     $response = new Response($options);
     $this->assertEquals('This is the body', $response->body());
     $this->assertEquals('my-custom-charset', $response->charset());
     $this->assertEquals('audio/mpeg', $response->type());
     $this->assertEquals(203, $response->statusCode());
     $options = ['body' => 'This is the body', 'charset' => 'my-custom-charset', 'type' => 'mp3', 'status' => '422', 'statusCodes' => [422 => 'Unprocessable Entity']];
     $response = new Response($options);
     $this->assertEquals($options['body'], $response->body());
     $this->assertEquals($options['charset'], $response->charset());
     $this->assertEquals($response->getMimeType($options['type']), $response->type());
     $this->assertEquals($options['status'], $response->statusCode());
 }
 /**
  * Maps a content type alias back to its mime-type(s)
  *
  * @param string|array $alias String alias to convert back into a content type. Or an array of aliases to map.
  * @return string Null on an undefined alias. String value of the mapped alias type. If an
  *   alias maps to more than one content type, the first one will be returned.
  */
 public function mapAlias($alias)
 {
     if (is_array($alias)) {
         return array_map(array($this, 'mapAlias'), $alias);
     }
     $type = $this->response->getMimeType($alias);
     if ($type) {
         if (is_array($type)) {
             return $type[0];
         }
         return $type;
     }
     return null;
 }