Cake\Network\Response::type PHP Method

type() public method

### Setting the content type type('jpg'); ### Returning the current content type type(); ### Storing content type definitions type(['keynote' => 'application/keynote', 'bat' => 'application/bat']); ### Replacing a content type definition type(['jpg' => 'text/plain']);
public type ( string | null $contentType = null ) : mixed
$contentType string | null Content type key.
return mixed Current content type or false if supplied an invalid content type
    public function type($contentType = null)
    {
        if ($contentType === null) {
            return $this->_contentType;
        }
        if (is_array($contentType)) {
            foreach ($contentType as $type => $definition) {
                $this->_mimeTypes[$type] = $definition;
            }
            return $this->_contentType;
        }
        if (isset($this->_mimeTypes[$contentType])) {
            $contentType = $this->_mimeTypes[$contentType];
            $contentType = is_array($contentType) ? current($contentType) : $contentType;
        }
        if (strpos($contentType, '/') === false) {
            return false;
        }
        return $this->_contentType = $contentType;
    }

Usage Example

 /**
  * Create the response.
  *
  * @param \League\Flysystem\FilesystemInterface $cache The cache file system.
  * @param string $path The cached file path.
  *
  * @return \Cake\Network\Response The response object.
  */
 public function create(FilesystemInterface $cache, $path)
 {
     $stream = $cache->readStream($path);
     $contentType = $cache->getMimetype($path);
     $contentLength = (string) $cache->getSize($path);
     $response = new Response();
     $response->type($contentType);
     $response->header('Content-Length', $contentLength);
     $response->body(function () use($stream) {
         rewind($stream);
         fpassthru($stream);
         fclose($stream);
     });
     return $response;
 }
All Usage Examples Of Cake\Network\Response::type