CakeRequest::accepts PHP Method

accepts() public method

#### Get all types: $this->request->accepts(); #### Check for a single type: $this->request->accepts('application/json'); This method will order the returned content types by the preference values indicated by the client.
public accepts ( string $type = null ) : mixed
$type string The content type to check for. Leave null to get all types a client accepts.
return mixed Either an array of all the types the client accepts or a boolean if they accept the provided type.
    public function accepts($type = null)
    {
        $raw = $this->parseAccept();
        $accept = array();
        foreach ($raw as $types) {
            $accept = array_merge($accept, $types);
        }
        if ($type === null) {
            return $accept;
        }
        return in_array($type, $accept);
    }

Usage Example

 /**
  * Determines which content types the client accepts.  Acceptance is based on
  * the file extension parsed by the Router (if present), and by the HTTP_ACCEPT
  * header. Unlike CakeRequest::accepts() this method deals entirely with mapped content types.
  *
  * Usage:
  *
  * `$this->RequestHandler->accepts(array('xml', 'html', 'json'));`
  *
  * Returns true if the client accepts any of the supplied types.
  *
  * `$this->RequestHandler->accepts('xml');`
  *
  * Returns true if the client accepts xml.
  *
  * @param mixed $type Can be null (or no parameter), a string type name, or an
  *   array of types
  * @return mixed If null or no parameter is passed, returns an array of content
  *   types the client accepts.  If a string is passed, returns true
  *   if the client accepts it.  If an array is passed, returns true
  *   if the client accepts one or more elements in the array.
  * @see RequestHandlerComponent::setContent()
  */
 public function accepts($type = null)
 {
     $accepted = $this->request->accepts();
     if ($type == null) {
         return $this->mapType($accepted);
     } elseif (is_array($type)) {
         foreach ($type as $t) {
             $t = $this->mapAlias($t);
             if (in_array($t, $accepted)) {
                 return true;
             }
         }
         return false;
     } elseif (is_string($type)) {
         $type = $this->mapAlias($type);
         return in_array($type, $accepted);
     }
     return false;
 }
All Usage Examples Of CakeRequest::accepts