lithium\action\Request::is PHP Method

is() public method

By default, the Request object is configured with several different types of assertions, which are individually known as _detectors_. Detectors are invoked by calling the is() and passing the name of the detector flag, i.e. $request->is(''), which returns true or false, depending on whether or not the the properties (usually headers or data) contained in the request match the detector. The default detectors include the following: - 'mobile': Uses a regular expression to match common mobile browser user agents. - 'ajax': Checks to see if the X-Requested-With header is present, and matches the value 'XMLHttpRequest'. - 'flash': Checks to see if the user agent is 'Shockwave Flash'. - 'ssl': Verifies that the request is SSL-secured. - 'get' / 'post' / 'put' / 'delete' / 'head' / 'options': Checks that the HTTP request method matches the one specified. In addition to the above, this method also accepts media type names (see Media::type()) to make assertions against the format of the request body (for POST or PUT requests), i.e. $request->is('json'). This will return true if the client has made a POST request with JSON data. For information about adding custom detectors or overriding the ones in the core, see the detect() method. While these detectors are useful in controllers or other similar contexts, they're also useful when performing _content negotiation_, which is the process of modifying the response format to suit the client (see the 'conditions' field of the $options parameter in Media::type()).
See also: lithium\action\Request::detect()
See also: lithium\net\http\Media::type()
public is ( string $flag ) : boolean
$flag string The name of the flag to check, which should be the name of a valid detector (that is either built-in or defined with `detect()`).
return boolean Returns `true` if the detector check succeeds (see the details for the built-in detectors above, or `detect()`), otherwise `false`.
    public function is($flag)
    {
        $media = $this->_classes['media'];
        if (!isset($this->_detectors[$flag])) {
            if (!in_array($flag, $media::types())) {
                return false;
            }
            return $this->type() === $flag;
        }
        $detector = $this->_detectors[$flag];
        if (!is_array($detector) && is_callable($detector)) {
            return $detector($this);
        }
        if (!is_array($detector)) {
            return (bool) $this->env($detector);
        }
        list($key, $check) = $detector + array('', '');
        if (is_array($check)) {
            $check = '/' . join('|', $check) . '/i';
        }
        if (Validator::isRegex($check)) {
            return (bool) preg_match($check, $this->env($key));
        }
        return $this->env($key) === $check;
    }

Usage Example

Example #1
0
 /**
  * Assists `Media::negotiate()` in processing the negotiation conditions of a content type, by
  * iterating through the conditions and checking each one against the `Request` object.
  *
  * @see lithium\net\http\Media::negotiate()
  * @see lithium\net\http\Media::type()
  * @see lithium\action\Request
  * @param \lithium\action\Request $request The request to be checked against a
  *        set of conditions (if applicable).
  * @param array $config Represents a content type configuration, which is an array containing 3
  *              keys:
  *              - `'name'` _string_: The type name, i.e. `'html'` or `'json'`.
  *              - `'content'` _mixed_: One or more content types that the configuration
  *                represents, i.e. `'text/html'`, `'application/xhtml+xml'` or
  *                `'application/json'`, or an array containing multiple content types.
  *              - `'options'` _array_: An array containing rendering information, and an
  *                optional `'conditions'` key, which contains an array of matching parameters.
  *                For more details on these matching parameters, see `Media::type()`.
  * @return boolean Returns `true` if the information in `$request` matches the type
  *         configuration in `$config`, otherwise false.
  */
 public static function match($request, array $config)
 {
     if (!isset($config['options']['conditions'])) {
         return true;
     }
     $conditions = $config['options']['conditions'];
     foreach ($conditions as $key => $value) {
         switch (true) {
             case $key === 'type':
                 if ($value !== ($request->type === $config['name'])) {
                     return false;
                 }
                 break;
             case strpos($key, ':'):
                 if ($request->get($key) !== $value) {
                     return false;
                 }
                 break;
             case $request->is($key) !== $value:
                 return false;
                 break;
         }
     }
     return true;
 }
All Usage Examples Of lithium\action\Request::is