diff options
author | Arnold Daniels <arnold@jasny.net> | 2014-06-19 13:54:20 +0200 |
---|---|---|
committer | Arnold Daniels <arnold@jasny.net> | 2014-06-19 13:54:20 +0200 |
commit | 3ad447e323b3a29a12afe25a5d18a84134d97d30 (patch) | |
tree | d688a42d8bbb8dd4378b9f3e06b5319e371f92f3 | |
parent | 1052ea3983fe71cd35b28d665d769952278323f7 (diff) | |
download | router-3ad447e323b3a29a12afe25a5d18a84134d97d30.zip router-3ad447e323b3a29a12afe25a5d18a84134d97d30.tar.gz router-3ad447e323b3a29a12afe25a5d18a84134d97d30.tar.bz2 |
Renamed methods getRequestFormat(), etc
-rw-r--r-- | src/Jasny/Controller.php | 34 | ||||
-rw-r--r-- | src/Jasny/Router.php | 31 |
2 files changed, 45 insertions, 20 deletions
diff --git a/src/Jasny/Controller.php b/src/Jasny/Controller.php index 531fbb5..89721ad 100644 --- a/src/Jasny/Controller.php +++ b/src/Jasny/Controller.php @@ -37,17 +37,6 @@ abstract class Controller } /** - * Check if we should output a specific format. - * Defaults to html. - * - * @return string 'html', 'json', 'xml', 'text', 'js', 'css', 'png', 'gif' or 'jpeg' - */ - protected function getRequestFormat() - { - return Router::getRequestFormat(); - } - - /** * Shortcut for REQUEST_METHOD === 'POST' * * @return boolean @@ -87,6 +76,27 @@ abstract class Controller return Router::getLocalReferer(); } + /** + * Get the requested output format. + * Defaults to html. + * + * @return string 'html', 'json', 'xml', 'text', 'js', 'css', 'png', 'gif' or 'jpeg' + */ + protected function getOutputFormat() + { + return isset($this->router) ? $this->router->getOutputFormat() : Router::getAcceptFormat(); + } + + /** + * Get the request input data, decoded based on Content-Type header. + * + * @return mixed + */ + protected function getInput() + { + return Router::getRequestData(); + } + /** * Show a view. @@ -97,7 +107,7 @@ abstract class Controller protected function view($name=null, $context=[]) { if (!isset($name) && isset($this->router)) - $name = $this->router()->get('controller') . '/' . $this->router()->get('action'); + $name = $this->router->get('controller') . '/' . $this->router->get('action'); View::load($name) ->set('current_route', $this->router->getRoute()) diff --git a/src/Jasny/Router.php b/src/Jasny/Router.php index 1f39d31..625fb97 100644 --- a/src/Jasny/Router.php +++ b/src/Jasny/Router.php @@ -189,7 +189,7 @@ class Router * * @return string 'html', 'json', 'xml', 'text', 'js', 'css', 'png', 'gif' or 'jpeg' */ - public static function getRequestFormat() + public static function getAcceptFormat() { if (empty($_SERVER['HTTP_ACCEPT'])) return 'html'; @@ -238,6 +238,21 @@ class Router $_SERVER['HTTP_HOST'] ? $_SERVER['HTTP_REFERER'] : null; } + /** + * Get the request input data, decoded based on Content-Type header. + * + * @return mixed + */ + public static function getRequestData() + { + switch ($_SERVER['CONTENT_TYPE']) { + case 'application/x-www-form-urlencoded': return $_POST; + case 'multipart/formdata': return $_FILES + $_POST; + case 'application/json': return json_decode(file_get_contents('php://input')); + case 'application/xml': return simplexml_load_string(file_get_contents('php://input')); + default: return file_get_contents('php://input'); + } + } /** * Set the method to route @@ -363,7 +378,7 @@ class Router */ public function getOutputFormat() { - if (!isset($this->format)) $this->format = self::getRequestFormat(); + if (!isset($this->format)) $this->format = self::getAcceptFormat(); return $this->format; } @@ -590,7 +605,7 @@ class Router */ public function badRequest($message, $http_code=400) { - if (self::getRequestFormat() !== 'html') { + if (self::getAcceptFormat() !== 'html') { self::outputError($http_code, $message, $this->getOutputFormat()); exit(); } @@ -623,7 +638,7 @@ class Router { if (!isset($message)) $message = "Sorry, you are not allowed to view this page"; - if (self::getRequestFormat() !== 'html') { + if (self::getAcceptFormat() !== 'html') { self::outputError($http_code, $message, $this->getOutputFormat()); exit(); } @@ -647,7 +662,7 @@ class Router $message = $http_code === 405 ? "Sorry, this action isn't supported" : "Sorry, this page does not exist"; } - if (self::getRequestFormat() !== 'html') { + if (self::getAcceptFormat() !== 'html') { self::outputError($http_code, $message, $this->getOutputFormat()); exit(); } @@ -679,7 +694,7 @@ class Router */ protected function _error($error, $http_code=500) { - if (self::getRequestFormat() !== 'html') { + if (self::getAcceptFormat() !== 'html') { self::outputError(500, $error, $this->getOutputFormat()); return true; } @@ -701,7 +716,7 @@ class Router { if (ob_get_level() > 1) ob_end_clean(); - if (!isset($format)) $format = static::isJsonpRequest() ? 'jsonp' : static::getRequestFormat(); + if (!isset($format)) $format = static::isJsonpRequest() ? 'jsonp' : static::getAcceptFormat(); if ($format !== 'jsonp') header(self::getProtocol() . ' ' . static::$httpStatusCodes[$http_code]); @@ -775,7 +790,7 @@ class Router */ protected static function outputErrorImage($format=null, $error=null) { - if (!isset($format)) $format = $this->getRequestFormat(); + if (!isset($format)) $format = $this->getAcceptFormat(); $image = imagecreate(100, 100); $black = imagecolorallocate($image, 0, 0, 0); |