Skip to content

Commit

Permalink
Added support for 301 and 302 redirects, enhanced 307, extended Contr…
Browse files Browse the repository at this point in the history
…oller to support.

Signed-off-by: Kris Jordan <krisjordan@gmail.com>

Signed-off-by: Recess PHP Framework <kris@recessframework.org>
  • Loading branch information
midnightmonster authored and recess committed Mar 11, 2009
1 parent 5643227 commit b18bfa8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
Expand Up @@ -45,12 +45,43 @@ protected function conflict($viewName) {
return $response;
}

protected function redirect($redirectUri) {
protected function redirect($redirectUri,$scheme=null) {
Library::import('recess.http.responses.TemporaryRedirectResponse');
$response = new TemporaryRedirectResponse($this->request, $redirectUri);
$response = new TemporaryRedirectResponse($this->request, $this->buildUrl($redirectUri,$scheme));
return $response;
}

protected function found($redirectUri,$scheme=null) {
Library::import('recess.http.responses.FoundResponse');
$response = new FoundResponse($this->request, $this->buildUrl($redirectUri,$scheme));
return $response;
}

protected function moved($redirectUri,$scheme=null) {
Library::import('recess.http.responses.MovedPermanentlyResponse');
$response = new MovedPermanentlyResponse($this->request, $this->buildUrl($redirectUri,$scheme));
return $response;
}

protected function buildUrl($uri,$scheme=null) {
$parts = parse_url($uri);
if(!is_null($scheme)) {
$parts['scheme'] = $scheme;
if(!empty($parts['host'])) $parts['host'] = $_SERVER['SERVER_NAME'];
}
$url = '';
if(!empty($parts['scheme'])) {
$url .= $parts['scheme'].'://';
if(!empty($parts['user'])) $url .= $parts['user'] . (empty($parts['pass']) ? '' : $parts['pass']) .'@';
$url .= $parts['host'];
if(!empty($parts['port'])) $url .= ':'.$parts['port'];
}
$url .= empty($parts['path']) ? '/' : $parts['path'];
if(!empty($parts['query'])) $url .= '?'.$parts['query'];
if(!empty($parts['fragment'])) $url .= '#'.$parts['fragment'];
return $url;
}

protected function forwardOk($forwardedUri) {
Library::import('recess.http.responses.ForwardingOkResponse');
return new ForwardingOkResponse($this->request, $forwardedUri);
Expand Down
11 changes: 11 additions & 0 deletions recess/lib/recess/http/responses/FoundResponse.class.php
@@ -0,0 +1,11 @@
<?php
Library::import('recess.http.Response');
Library::import('recess.http.ResponseCodes');

class FoundResponse extends Response {
public function __construct(Request $request, $resourceUri, $data = array()) {
parent::__construct($request, ResponseCodes::HTTP_FOUND, $data);
$this->addHeader("Location: $resourceUri");
}
}
?>
@@ -0,0 +1,11 @@
<?php
Library::import('recess.http.Response');
Library::import('recess.http.ResponseCodes');

class MovedPermanentlyResponse extends Response {
public function __construct(Request $request, $resourceUri, $data = array()) {
parent::__construct($request, ResponseCodes::HTTP_MOVED_PERMANENTLY, $data);
$this->addHeader("Location: $resourceUri");
}
}
?>
Expand Up @@ -5,7 +5,7 @@
class TemporaryRedirectResponse extends Response {
public function __construct(Request $request, $resourceUri, $data = array()) {
parent::__construct($request, ResponseCodes::HTTP_TEMPORARY_REDIRECT, $data);
$this->addHeader('Location: http://' . $_SERVER['SERVER_NAME'] . $resourceUri); // TODO: This should not reference localhost!
$this->addHeader("Location: $resourceUri");
}
}
?>

0 comments on commit b18bfa8

Please sign in to comment.