diff --git a/lib/Cake/Network/CakeRequest.php b/lib/Cake/Network/CakeRequest.php index 95915e521..4c4188459 100644 --- a/lib/Cake/Network/CakeRequest.php +++ b/lib/Cake/Network/CakeRequest.php @@ -104,7 +104,8 @@ class CakeRequest implements ArrayAccess { 'J2ME', 'MIDP', 'NetFront', 'Nokia', 'Opera Mini', 'PalmOS', 'PalmSource', 'portalmmm', 'Plucker', 'ReqwirelessWeb', 'SonyEricsson', 'Symbian', 'UP\\.Browser', 'webOS', 'Windows CE', 'Xiino' - )) + )), + 'requested' => array('param' => 'requested', 'value' => 1) ); /** @@ -435,6 +436,11 @@ class CakeRequest implements ArrayAccess { return (bool)preg_match($pattern, env($detect['env'])); } } + if (isset($detect['param'])) { + $key = $detect['param']; + $value = $detect['value']; + return isset($this->params[$key]) ? $this->params[$key] == $value : false; + } if (isset($detect['callback']) && is_callable($detect['callback'])) { return call_user_func($detect['callback'], $this); } @@ -472,6 +478,12 @@ class CakeRequest implements ArrayAccess { * * e.g `addDetector('custom', array('callback' => array('SomeClass', 'somemethod')));` * + * ### Request parameter detectors + * + * Allows for custom detectors on the request parameters. + * + * e.g `addDetector('post', array('param' => 'requested', 'value' => 1)` + * * @param string $name The name of the detector. * @param array $options The options for the detector definition. See above. * @return void diff --git a/lib/Cake/Test/Case/Network/CakeRequestTest.php b/lib/Cake/Test/Case/Network/CakeRequestTest.php index acd0ba2a2..b9e81274a 100644 --- a/lib/Cake/Test/Case/Network/CakeRequestTest.php +++ b/lib/Cake/Test/Case/Network/CakeRequestTest.php @@ -744,6 +744,13 @@ class CakeRequestTest extends CakeTestCase { $request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, '_detectCallback'))); + $request->addDetector('index', array('param' => 'action', 'value' => 'index')); + $request->params['action'] = 'index'; + $this->assertTrue($request->isIndex()); + + $request->params['action'] = 'add'; + $this->assertFalse($request->isIndex()); + $request->return = true; $this->assertTrue($request->isCallMe()); @@ -1565,6 +1572,32 @@ XML; ); } +/** + * Test is('requested') and isRequested() + * + * @return void + */ + public function testIsRequested() { + $request = new CakeRequest('/posts/index'); + $request->addParams(array( + 'controller' => 'posts', + 'action' => 'index', + 'plugin' => null, + 'requested' => 1 + )); + $this->assertTrue($request->is('requested')); + $this->assertTrue($request->isRequested()); + + $request = new CakeRequest('/posts/index'); + $request->addParams(array( + 'controller' => 'posts', + 'action' => 'index', + 'plugin' => null, + )); + $this->assertFalse($request->is('requested')); + $this->assertFalse($request->isRequested()); + } + /** * loadEnvironment method *