Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update to new Restful controller format

  • Loading branch information...
commit dc364633ea475441fbfc70a94d3a44f9dfab5b44 1 parent 60d7cc8
Mior Muhammad Zaki crynobone authored
20 classes/controller.php
View
@@ -58,6 +58,16 @@
* @var bool
*/
protected $set_content_type = true;
+
+ /**
+ * @var integer status code to return in case a not defined action is called
+ */
+ protected $no_method_status = 405;
+
+ /**
+ * @var integer status code to return in case the called action doesn't return data
+ */
+ protected $no_data_status = 204;
/**
* Page template
@@ -174,7 +184,7 @@ public function router($resource, $arguments)
{
if (true === $this->rest)
{
- $this->response->status = 404;
+ $this->response->status = $this->no_method_status;
return;
}
@@ -184,7 +194,7 @@ public function router($resource, $arguments)
{
if (true === $this->rest)
{
- $this->response->status = 404;
+ $this->response->status = $this->no_method_status;
return;
}
else
@@ -206,6 +216,12 @@ protected function response($data = array(), $http_code = 200)
{
if (true === $this->rest)
{
+ if ((is_array($data) and empty($data)) or ($data == ''))
+ {
+ $this->response->status = $this->no_data_status;
+ return;
+ }
+
$rest_server = Restserver::make($data, $http_code)
->format($this->rest_format)
->execute();
18 classes/controller/rest.php
View
@@ -47,6 +47,16 @@
protected $set_content_type = true;
/**
+ * @var integer status code to return in case a not defined action is called
+ */
+ protected $no_method_status = 405;
+
+ /**
+ * @var integer status code to return in case the called action doesn't return data
+ */
+ protected $no_data_status = 204;
+
+ /**
* Run ACL check and redirect user automatically if user doesn't have the privilege
*
* @final
@@ -135,7 +145,7 @@ public function router($resource, $arguments)
}
else
{
- $this->response->status = 404;
+ $this->response->status = $this->no_method_status;
return ;
}
}
@@ -149,6 +159,12 @@ public function router($resource, $arguments)
*/
protected function response($data = array(), $http_code = 200)
{
+ if ((is_array($data) and empty($data)) or ($data == ''))
+ {
+ $this->response->status = $this->no_data_status;
+ return;
+ }
+
$rest_server = Restserver::make($data, $http_code)
->format($this->rest_format)
->execute();
2  classes/input.php
View
@@ -74,7 +74,7 @@ public static function disconnect()
public static function __callStatic($name, $arguments)
{
// If $request is null, it's a request from \Fuel\Core\Request so use it instead
- if (in_array(strtolower($name), array('is_ajax', 'protocol', 'real_ip', 'referrer', 'server', 'uri', 'user_agent')))
+ if (in_array(strtolower($name), array('is_ajax', 'protocol', 'real_ip', 'referrer', 'server', 'uri', 'user_agent', 'extension')))
{
return call_user_func_array(array("Fuel\Core\Input", $name), $arguments);
}
7 classes/restserver.php
View
@@ -15,6 +15,7 @@
use \Config;
use \Format;
+use \Input;
use \FuelException;
use \stdClass;
@@ -109,11 +110,11 @@ public static function __callStatic($method, array $arguments)
*/
public static function is_rest()
{
- $pattern = static::$pattern;
- $resource = \Request::active()->action;
+ $pattern = static::$pattern;
+ $extension = Input::extension();
// Check if a file extension is used
- return preg_match($pattern, $resource, $matches) or '' != static::detect_format();
+ return preg_match($pattern, $extension, $matches) or '' != static::detect_format();
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.