Skip to content

Commit 103bbbc

Browse files
committed
Add CakeRequest::param()
This method gives a read accessor to the data in $request->params. It removes the need to use isset() and empty().
1 parent 80b8e7d commit 103bbbc

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/Cake/Network/CakeRequest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,20 @@ public function data($name) {
805805
return Hash::get($this->data, $name);
806806
}
807807

808+
/**
809+
* Safely access the values in $this->params.
810+
*
811+
* @param string $name The name of the parameter to get.
812+
* @return mixed The value of the provided parameter. Will
813+
* return false if the parameter doesn't exist or is falsey.
814+
*/
815+
public function param($name) {
816+
if (!isset($this->params[$name])) {
817+
return false;
818+
}
819+
return $this->params[$name];
820+
}
821+
808822
/**
809823
* Read data from `php://input`. Useful when interacting with XML or JSON
810824
* request body content.

lib/Cake/Test/Case/Network/CakeRequestTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,26 @@ public function testQueryWithArray() {
17681768
$this->assertNull($result);
17691769
}
17701770

1771+
/**
1772+
* Test using param()
1773+
*
1774+
* @return void
1775+
*/
1776+
public function testReadingParams() {
1777+
$request = new CakeRequest();
1778+
$request->addParams(array(
1779+
'controller' => 'posts',
1780+
'admin' => true,
1781+
'truthy' => 1,
1782+
'zero' => '0',
1783+
));
1784+
$this->assertFalse($request->param('not_set'));
1785+
$this->assertTrue($request->param('admin'));
1786+
$this->assertEquals(1, $request->param('truthy'));
1787+
$this->assertEquals('posts', $request->param('controller'));
1788+
$this->assertEquals('0', $request->param('zero'));
1789+
}
1790+
17711791
/**
17721792
* test the data() method reading
17731793
*

0 commit comments

Comments
 (0)