lithium\net\http\Message::type PHP Method

type() public method

Sets/gets the content type.
public type ( string $type = null ) : string
$type string A full content type i.e. `'application/json'` or simple name `'json'`
return string A simple content type name, i.e. `'html'`, `'xml'`, `'json'`, etc., depending on the content type of the request.
    public function type($type = null)
    {
        if ($type === false) {
            unset($this->headers['Content-Type']);
            $this->_type = false;
            return;
        }
        $media = $this->_classes['media'];
        if (!$type && $this->_type) {
            return $this->_type;
        }
        $headers = $this->headers + array('Content-Type' => null);
        $type = $type ?: $headers['Content-Type'];
        if (!$type) {
            return;
        }
        $header = $type;
        if (!($data = $media::type($type))) {
            $this->headers('Content-Type', $type);
            return $this->_type = $type;
        }
        if (is_string($data)) {
            $type = $data;
        } elseif (!empty($data['content'])) {
            $header = is_string($data['content']) ? $data['content'] : reset($data['content']);
        }
        $this->headers('Content-Type', $header);
        return $this->_type = $type;
    }

Usage Example

Beispiel #1
0
 /**
  * Sets/Gets the content type. If `'type'` is null, the method will attempt to determine the
  * type first, from the params, then from the environment setting
  *
  * @param string $type a full content type i.e. `'application/json'` or simple name `'json'`
  * @return string A simple content type name, i.e. `'html'`, `'xml'`, `'json'`, etc., depending
  *         on the content type of the request.
  */
 public function type($type = null)
 {
     if ($type === null) {
         $type = $this->type;
         if (!$type) {
             $type = $this->env('CONTENT_TYPE');
         }
     }
     return parent::type($type);
 }