lithium\action\Request::accepts PHP Méthode

accepts() public méthode

This method may work different then you might think. This is a _convenience_ method working exclusively with short type names it knows about. Only those types will be matched. You can tell this method about more types via Media::type(). Note: In case negotiation fails, 'html' is used as a fallback type.
See also: lithium\net\http\Media::negotiate()
public accepts ( boolean | string $type = null ) : string | boolean | array
$type boolean | string Optionally a type name i.e. `'json'` or `true`. 1. If not specified, returns the media type name that the client prefers, using a potentially set `type` param, then content negotiation and that fails, ultimately falling back and returning the string `'html'`. 2. If a media type name (string) is passed, returns `true` or `false`, indicating whether or not that type is accepted by the client at all. 3. If `true`, returns the raw content types from the `Accept` header, parsed into an array and sorted by client preference.
Résultat string | boolean | array Returns a type name (i.e. 'json'`) or a boolean or an array, depending on the value of `$type`.
    public function accepts($type = null)
    {
        $media = $this->_classes['media'];
        if ($type === true) {
            return $this->_accept ?: ($this->_accept = $this->_parseAccept());
        }
        if ($type) {
            return ($media::negotiate($this) ?: 'html') === $type;
        }
        if (isset($this->params['type'])) {
            return $this->params['type'];
        }
        return $media::negotiate($this) ?: 'html';
    }

Usage Example

Exemple #1
0
 /**
  * Tests that `Accept` headers with only one listed content type are parsed property, and tests
  * that `'* /*'` is still parsed as `'text/html'`.
  */
 public function testAcceptSingleContentType()
 {
     $request = new Request(array('env' => array('HTTP_ACCEPT' => 'application/json,text/xml')));
     $this->assertEqual(array('application/json', 'text/xml'), $request->accepts(true));
     $this->assertEqual('json', $request->accepts());
     $request = new Request(array('env' => array('HTTP_ACCEPT' => 'application/json')));
     $this->assertEqual(array('application/json'), $request->accepts(true));
     $this->assertEqual('json', $request->accepts());
     $request = new Request(array('env' => array('HTTP_ACCEPT' => '*/*')));
     $this->assertEqual(array('text/html'), $request->accepts(true));
     $this->assertEqual('html', $request->accepts());
 }
All Usage Examples Of lithium\action\Request::accepts