Skip to content

Commit

Permalink
Added default value to \Slim\Http\Request::params
Browse files Browse the repository at this point in the history
  • Loading branch information
José Miguel Molina Arboledas authored and Josh Lockhart committed Apr 5, 2014
1 parent 5809920 commit ef3fb71
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Slim/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,18 @@ public function isXhr()
* Fetch GET and POST data
*
* This method returns a union of GET and POST data as a key-value array, or the value
* of the array key if requested; if the array key does not exist, NULL is returned.
* of the array key if requested; if the array key does not exist, NULL is returned,
* unless there is a default value specified.
*
* @param string $key
* @param mixed $default
* @return array|mixed|null
*/
public function params($key = null)
public function params($key = null, $default = null)
{
$union = array_merge($this->get(), $this->post());
if ($key) {
return isset($union[$key]) ? $union[$key] : null;
return isset($union[$key]) ? $union[$key] : $default;
}

return $union;
Expand Down
1 change: 1 addition & 0 deletions tests/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public function testParamsFromQueryString()
$this->assertEquals(3, count($req->params()));
$this->assertEquals('1', $req->params('one'));
$this->assertNull($req->params('foo'));
$this->assertEquals(1, $req->params('foo', 1));
}

/**
Expand Down

0 comments on commit ef3fb71

Please sign in to comment.