diff options
author | Arnold Daniels <arnold@jasny.net> | 2013-11-28 19:11:20 +0100 |
---|---|---|
committer | Arnold Daniels <arnold@jasny.net> | 2013-11-28 19:11:20 +0100 |
commit | 8d17d9d5b81d80f099f12bb6be9613f945422eef (patch) | |
tree | 470effa30a772acbf8f3c6015562a7b0413c97c5 | |
parent | af7ca1fa819292355aec6558d4c994972aa6d148 (diff) | |
download | router-8d17d9d5b81d80f099f12bb6be9613f945422eef.zip router-8d17d9d5b81d80f099f12bb6be9613f945422eef.tar.gz router-8d17d9d5b81d80f099f12bb6be9613f945422eef.tar.bz2 |
Pass Router to Controller
Make Controller::redirect, notfound and badrequest work without a router
-rw-r--r-- | src/Jasny/Controller.php | 43 | ||||
-rw-r--r-- | src/Jasny/Router.php | 10 |
2 files changed, 45 insertions, 8 deletions
diff --git a/src/Jasny/Controller.php b/src/Jasny/Controller.php index 7ec486f..9cb89d4 100644 --- a/src/Jasny/Controller.php +++ b/src/Jasny/Controller.php @@ -7,6 +7,22 @@ namespace Jasny; abstract class Controller { /** + * Used router + * @var Router + */ + protected $router; + + /** + * Class constructor + * + * @param Router $router + */ + public function __construct($router=null) + { + $this->router = $router; + } + + /** * Show a view. * * @param string $name Filename of Twig template @@ -29,7 +45,18 @@ abstract class Controller */ protected function redirect($url, $http_code = 303) { - return Router::i()->redirect($url, $http_code); + if ($this->router) return $this->router->redirect($url, $http_code); + + // Turn relative URL into absolute URL + if (strpos($url, '://') === false) { + if ($url == '' || $url[0] != '/') $url = dirname($_SERVER['REQUEST_URI']) . '/' . $url; + $url = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://') . $_SERVER['HTTP_HOST'] . $this->rebase($url); + } + + header("Location: $url", true, $http_code); + echo 'You are being redirected to <a href="' . $url . '">' . $url . '</a>'; + + exit(); } /** @@ -39,7 +66,11 @@ abstract class Controller */ protected function notFound($message=null) { - return Router::i()->notFound($message); + if ($this->router) return $this->router->notFound($message); + + header((@$_SERVER['SERVER_PROTOCOL'] ?: 'HTTP/1.1') . ' 404 Not Found'); + echo $message ?: "Sorry, this page does not exist"; + exit(); } /** @@ -49,7 +80,13 @@ abstract class Controller */ protected function badRequest($message) { - return Router::i()->badRequest($message); + if ($this->router) return $this->router->badRequest($message); + + if (ob_get_level() > 1) ob_end_clean(); + + header((@$_SERVER['SERVER_PROTOCOL'] ?: 'HTTP/1.1') . ' 400 Bad Request'); + echo $message; + exit(); } diff --git a/src/Jasny/Router.php b/src/Jasny/Router.php index cb6541d..91aa0f4 100644 --- a/src/Jasny/Router.php +++ b/src/Jasny/Router.php @@ -257,7 +257,7 @@ class Router if (!class_exists($class)) return $this->notFound(); - $controller = new $class(); + $controller = new $class($this); if (!is_callable([$controller, $method])) return $this->notFound(); $ret = call_user_func_array([$controller, $method], $args); @@ -312,7 +312,7 @@ class Router * * @return string; */ - protected function getProtocol() + protected static function getProtocol() { return @$_SERVER['SERVER_PROTOCOL'] ?: 'HTTP/1.1'; } @@ -348,7 +348,7 @@ class Router { if (ob_get_level() > 1) ob_end_clean(); - header($this->getProtocol() . ' 400 Bad Request'); + header(self::getProtocol() . ' 400 Bad Request'); if (!$this->routeTo(400, ['args'=>[400, $message]])) echo $message; exit(); } @@ -364,7 +364,7 @@ class Router if (!isset($message)) $message = "Sorry, this page does not exist"; - header($this->getProtocol() . ' 404 Not Found'); + header(self::getProtocol() . ' 404 Not Found'); if (!$this->routeTo(404, ['args'=>[404, $message]])) echo $message; exit(); } @@ -379,7 +379,7 @@ class Router { if (ob_get_level() > 1) ob_end_clean(); - header($this->getProtocol() . ' 500 Internal Server Error'); + header(self::getProtocol() . ' 500 Internal Server Error'); return (boolean)$this->routeTo(500, ['args'=>[500, $error]]); } |