Add CakeResponse::location()

This method provides an easy to use interface to get/set the location
header in a response object. This is primarily to facilitate future
development in 3.x
This commit is contained in:
mark_story 2013-07-16 23:38:57 -04:00
parent 72508cb260
commit 2df873412c
2 changed files with 28 additions and 0 deletions

View file

@ -584,6 +584,22 @@ class CakeResponse {
return $this->_headers;
}
/**
* Acccessor for the location header.
*
* Get/Set the Location header value.
* @param null|string $url Either null to get the current location, or a string to set one.
* @return string|null When setting the location null will be returned. When reading the location
* a string of the current location header value (if any) will be returned.
*/
public function location($url = null) {
if ($url === null) {
$headers = $this->header();
return isset($headers['Location']) ? $headers['Location'] : null;
}
$this->header('Location', $url);
}
/**
* Buffers the response message to be sent
* if $content is null the current buffer is returned

View file

@ -1481,4 +1481,16 @@ class CakeResponseTest extends CakeTestCase {
$result = $response->send();
}
/**
* Test the location method.
*
* @return void
*/
public function testLocation() {
$response = new CakeResponse();
$this->assertNull($response->location());
$this->assertNull($response->location('http://example.org'));
$this->assertEquals('http://example.org', $response->location());
}
}