Permalink
Browse files

Add CakeRequest::param()

This method gives a read accessor to the data in $request->params.
It removes the need to use isset() and empty().
  • Loading branch information...
1 parent 80b8e7d commit 103bbbc3751b8162a28d140661cca050532764b7 @markstory markstory committed Feb 4, 2013
Showing with 34 additions and 0 deletions.
  1. +14 −0 lib/Cake/Network/CakeRequest.php
  2. +20 −0 lib/Cake/Test/Case/Network/CakeRequestTest.php
@@ -806,6 +806,20 @@ public function data($name) {
}
/**
+ * Safely access the values in $this->params.
+ *
+ * @param string $name The name of the parameter to get.
+ * @return mixed The value of the provided parameter. Will
+ * return false if the parameter doesn't exist or is falsey.
+ */
+ public function param($name) {
+ if (!isset($this->params[$name])) {
+ return false;
+ }
+ return $this->params[$name];
+ }
+
+/**
* Read data from `php://input`. Useful when interacting with XML or JSON
* request body content.
*
@@ -1769,6 +1769,26 @@ public function testQueryWithArray() {
}
/**
+ * Test using param()
+ *
+ * @return void
+ */
+ public function testReadingParams() {
+ $request = new CakeRequest();
+ $request->addParams(array(
+ 'controller' => 'posts',
+ 'admin' => true,
+ 'truthy' => 1,
+ 'zero' => '0',
+ ));
+ $this->assertFalse($request->param('not_set'));
+ $this->assertTrue($request->param('admin'));
+ $this->assertEquals(1, $request->param('truthy'));
+ $this->assertEquals('posts', $request->param('controller'));
+ $this->assertEquals('0', $request->param('zero'));
+ }
+
+/**
* test the data() method reading
*
* @return void

0 comments on commit 103bbbc

Please sign in to comment.