detect() public method

A detector check can be either an exact string match or a regular expression match against a header or environment variable. A detector check can also be a closure that accepts the Request object instance as a parameter. For example, to detect whether a request is from an iPhone, you can do the following: embed:lithium\tests\cases\action\RequestTest::testDetect(11-12)
See also: lithium\action\Request::is()
public detect ( string $flag, mixed $detector = null ) : void
$flag string The name of the detector check. Used in subsequent calls to `Request::is()`.
$detector mixed Detectors can be specified in four different ways: - The name of an HTTP header or environment variable. If a string, calling the detector will check that the header or environment variable exists and is set to a non-empty value. - A two-element array containing a header/environment variable name, and a value to match against. The second element of the array must be an exact match to the header or variable value. - A two-element array containing a header/environment variable name, and a regular expression that matches against the value, as in the example above. - A closure which accepts an instance of the `Request` object and returns a boolean value.
return void
Example #1
0
 public function testDetectWithArrayRegex()
 {
     $request = new Request(array('env' => array('SOME_COOL_DETECTION' => 'this is cool')));
     $request->detect('cool', array('SOME_COOL_DETECTION', '/cool/'));
     $result = $request->is('cool');
     $this->assertTrue($result);
 }
 public function testDetectWithArrayRegex()
 {
     $_SERVER['SOME_COOL_DETECTION'] = 'this is cool';
     $request = new Request();
     $request->detect('cool', array('SOME_COOL_DETECTION', '/cool/'));
     $expected = true;
     $result = $request->is('cool');
     $this->assertEqual($expected, $result);
 }