Skip to content

Instantly share code, notes, and snippets.

@kiall
Created January 12, 2012 02:21
Show Gist options
  • Save kiall/1598163 to your computer and use it in GitHub Desktop.
Save kiall/1598163 to your computer and use it in GitHub Desktop.
diff --git a/classes/http/exception/300.php b/classes/http/exception/300.php
deleted file mode 100644
index 717628d..0000000
--- a/classes/http/exception/300.php
+++ /dev/null
@@ -1,3 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-class HTTP_Exception_300 extends Kohana_HTTP_Exception_300 {}
\ No newline at end of file
diff --git a/classes/http/exception/301.php b/classes/http/exception/301.php
deleted file mode 100644
index d916fac..0000000
--- a/classes/http/exception/301.php
+++ /dev/null
@@ -1,3 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-class HTTP_Exception_301 extends Kohana_HTTP_Exception_301 {}
\ No newline at end of file
diff --git a/classes/http/exception/302.php b/classes/http/exception/302.php
deleted file mode 100644
index 3602c61..0000000
--- a/classes/http/exception/302.php
+++ /dev/null
@@ -1,3 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-class HTTP_Exception_302 extends Kohana_HTTP_Exception_302 {}
\ No newline at end of file
diff --git a/classes/http/exception/303.php b/classes/http/exception/303.php
deleted file mode 100644
index 387464f..0000000
--- a/classes/http/exception/303.php
+++ /dev/null
@@ -1,3 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-class HTTP_Exception_303 extends Kohana_HTTP_Exception_303 {}
\ No newline at end of file
diff --git a/classes/http/exception/305.php b/classes/http/exception/305.php
deleted file mode 100644
index d3fa3fd..0000000
--- a/classes/http/exception/305.php
+++ /dev/null
@@ -1,3 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-class HTTP_Exception_305 extends Kohana_HTTP_Exception_305 {}
\ No newline at end of file
diff --git a/classes/http/exception/307.php b/classes/http/exception/307.php
deleted file mode 100644
index c1f7bc7..0000000
--- a/classes/http/exception/307.php
+++ /dev/null
@@ -1,3 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-class HTTP_Exception_307 extends Kohana_HTTP_Exception_307 {}
\ No newline at end of file
diff --git a/classes/http/exception/redirect.php b/classes/http/exception/redirect.php
deleted file mode 100644
index 7ec1809..0000000
--- a/classes/http/exception/redirect.php
+++ /dev/null
@@ -1,3 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-abstract class HTTP_Exception_Redirect extends Kohana_HTTP_Exception_Redirect {}
\ No newline at end of file
diff --git a/classes/kohana/controller.php b/classes/kohana/controller.php
index 6f93c7b..d3344fa 100644
--- a/classes/kohana/controller.php
+++ b/classes/kohana/controller.php
@@ -114,6 +114,24 @@ abstract class Kohana_Controller {
}
/**
+ * Marks up a Response with the approperiate HTTP headers and status code
+ * to redirect the user-agent.
+ *
+ * [!!] This does not `exit()`, any code after this call (and in the `after()` method) will continue to run. You need to handle that!
+ *
+ * $this->redirect('account/login', 303);
+ * return;
+ *
+ * @param string $uri URI to redirect to
+ * @param int $code Status code (eg 300, 301, 302, 303, 305, 307)
+ * @return Response
+ */
+ protected function redirect($uri, $code = 302)
+ {
+ return HTTP::redirect($this->request, $this->response, $uri, $code);
+ }
+
+ /**
* Checks the browser cache to see the response needs to be returned,
* execution will halt and a 304 Not Modified will be sent if the
* browser cache is up to date.
diff --git a/classes/kohana/http.php b/classes/kohana/http.php
index 42a47b9..eb45884 100644
--- a/classes/kohana/http.php
+++ b/classes/kohana/http.php
@@ -22,6 +22,29 @@ abstract class Kohana_HTTP {
public static $protocol = 'HTTP/1.1';
/**
+ * Marks up a Response with the approperiate HTTP headers and status code
+ * to redirect the user-agent.
+ *
+ * HTTP::redirect($response, 'account/login', 302);
+ *
+ * @param Response $response Response to set redirect on
+ * @param string $uri URI to redirect to
+ * @param int $code Status code (eg 300, 301, 302, 303, 305, 307)
+ * @return Response
+ */
+ public static function redirect(Response $response, $uri, $code = 302)
+ {
+ if (strpos($uri, '://') === FALSE)
+ {
+ // Make the URI into a URL
+ $uri = URL::site($uri, TRUE, ! empty(Kohana::$index_file));
+ }
+
+ return $response->status($code)
+ ->headers('Location', $uri);
+ }
+
+ /**
* Checks the browser cache to see the response needs to be returned,
* execution will halt and a 304 Not Modified will be sent if the
* browser cache is up to date.
diff --git a/classes/kohana/http/exception/300.php b/classes/kohana/http/exception/300.php
deleted file mode 100644
index 5b371ad..0000000
--- a/classes/kohana/http/exception/300.php
+++ /dev/null
@@ -1,10 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-class Kohana_HTTP_Exception_300 extends HTTP_Exception_Redirect {
-
- /**
- * @var integer HTTP 300 Multiple Choices
- */
- protected $_code = 300;
-
-}
\ No newline at end of file
diff --git a/classes/kohana/http/exception/301.php b/classes/kohana/http/exception/301.php
deleted file mode 100644
index fe8cfd8..0000000
--- a/classes/kohana/http/exception/301.php
+++ /dev/null
@@ -1,10 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-class Kohana_HTTP_Exception_301 extends HTTP_Exception_Redirect {
-
- /**
- * @var integer HTTP 301 Moved Permanently
- */
- protected $_code = 301;
-
-}
\ No newline at end of file
diff --git a/classes/kohana/http/exception/302.php b/classes/kohana/http/exception/302.php
deleted file mode 100644
index ee10ff0..0000000
--- a/classes/kohana/http/exception/302.php
+++ /dev/null
@@ -1,10 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-class Kohana_HTTP_Exception_302 extends HTTP_Exception_Redirect {
-
- /**
- * @var integer HTTP 302 Found
- */
- protected $_code = 302;
-
-}
\ No newline at end of file
diff --git a/classes/kohana/http/exception/303.php b/classes/kohana/http/exception/303.php
deleted file mode 100644
index 3256e32..0000000
--- a/classes/kohana/http/exception/303.php
+++ /dev/null
@@ -1,10 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-class Kohana_HTTP_Exception_303 extends HTTP_Exception_Redirect {
-
- /**
- * @var integer HTTP 303 See Other
- */
- protected $_code = 303;
-
-}
\ No newline at end of file
diff --git a/classes/kohana/http/exception/305.php b/classes/kohana/http/exception/305.php
deleted file mode 100644
index 6eb1f43..0000000
--- a/classes/kohana/http/exception/305.php
+++ /dev/null
@@ -1,41 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-class Kohana_HTTP_Exception_305 extends HTTP_Exception_Expected {
-
- /**
- * @var integer HTTP 305 Use Proxy
- */
- protected $_code = 305;
-
- /**
- * Specifies the proxy to replay this request via
- *
- * @param string $location URI of the proxy
- */
- public function location($uri = NULL)
- {
- if ($uri === NULL)
- return $this->headers('Location');
-
- $this->headers('Location', $uri);
-
- return $this;
- }
-
- /**
- * Validate this exception contains everything needed to continue.
- *
- * @throws Kohana_Exception
- * @return bool
- */
- public function check()
- {
- if ($location = $this->headers('location') === NULL)
- throw new Kohana_Exception('A \'location\' must be specified for a redirect');
-
- if (strpos($location, '://') === FALSE)
- throw new Kohana_Exception('An absolute URI to the proxy server must be specified');
-
- return TRUE;
- }
-}
\ No newline at end of file
diff --git a/classes/kohana/http/exception/307.php b/classes/kohana/http/exception/307.php
deleted file mode 100644
index 1b3a4cf..0000000
--- a/classes/kohana/http/exception/307.php
+++ /dev/null
@@ -1,10 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-class Kohana_HTTP_Exception_307 extends HTTP_Exception_Redirect {
-
- /**
- * @var integer HTTP 307 Temporary Redirect
- */
- protected $_code = 307;
-
-}
\ No newline at end of file
diff --git a/classes/kohana/http/exception/redirect.php b/classes/kohana/http/exception/redirect.php
deleted file mode 100644
index 8a72873..0000000
--- a/classes/kohana/http/exception/redirect.php
+++ /dev/null
@@ -1,51 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-/**
- * Redirect HTTP exception class. Used for all [HTTP_Exception]'s where the status
- * code indicates a redirect.
- *
- * Eg [HTTP_Exception_301], [HTTP_Exception_302] and most of the other 30x's
- *
- * @package Kohana
- * @category Exceptions
- * @author Kohana Team
- * @copyright (c) 2008-2012 Kohana Team
- * @license http://kohanaframework.org/license
- */
-abstract class Kohana_HTTP_Exception_Redirect extends HTTP_Exception_Expected {
-
- /**
- * Specifies the URI to redirect to.
- *
- * @param string $location URI of the proxy
- */
- public function location($uri = NULL)
- {
- if ($uri === NULL)
- return $this->headers('Location');
-
- if (strpos($uri, '://') === FALSE)
- {
- // Make the URI into a URL
- $uri = URL::site($uri, TRUE, ! empty(Kohana::$index_file));
- }
-
- $this->headers('Location', $uri);
-
- return $this;
- }
-
- /**
- * Validate this exception contains everything needed to continue.
- *
- * @throws Kohana_Exception
- * @return bool
- */
- public function check()
- {
- if ($this->headers('location') === NULL)
- throw new Kohana_Exception('A \'location\' must be specified for a redirect');
-
- return TRUE;
- }
-
-} // End Kohana_HTTP_Exception_Redirect
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment