Skip to content

Commit

Permalink
Update to new Restful controller format
Browse files Browse the repository at this point in the history
  • Loading branch information
crynobone committed Apr 13, 2012
1 parent 60d7cc8 commit dc36463
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
20 changes: 18 additions & 2 deletions classes/controller.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ abstract class Controller extends \Fuel\Core\Controller
* @var bool * @var bool
*/ */
protected $set_content_type = true; 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 * Page template
Expand Down Expand Up @@ -174,7 +184,7 @@ public function router($resource, $arguments)
{ {
if (true === $this->rest) if (true === $this->rest)
{ {
$this->response->status = 404; $this->response->status = $this->no_method_status;
return; return;
} }


Expand All @@ -184,7 +194,7 @@ public function router($resource, $arguments)
{ {
if (true === $this->rest) if (true === $this->rest)
{ {
$this->response->status = 404; $this->response->status = $this->no_method_status;
return; return;
} }
else else
Expand All @@ -206,6 +216,12 @@ protected function response($data = array(), $http_code = 200)
{ {
if (true === $this->rest) 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) $rest_server = Restserver::make($data, $http_code)
->format($this->rest_format) ->format($this->rest_format)
->execute(); ->execute();
Expand Down
18 changes: 17 additions & 1 deletion classes/controller/rest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ abstract class Controller_Rest extends \Fuel\Core\Controller
*/ */
protected $set_content_type = true; 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 * Run ACL check and redirect user automatically if user doesn't have the privilege
* *
Expand Down Expand Up @@ -135,7 +145,7 @@ public function router($resource, $arguments)
} }
else else
{ {
$this->response->status = 404; $this->response->status = $this->no_method_status;
return ; return ;
} }
} }
Expand All @@ -149,6 +159,12 @@ public function router($resource, $arguments)
*/ */
protected function response($data = array(), $http_code = 200) 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) $rest_server = Restserver::make($data, $http_code)
->format($this->rest_format) ->format($this->rest_format)
->execute(); ->execute();
Expand Down
2 changes: 1 addition & 1 deletion classes/input.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static function disconnect()
public static function __callStatic($name, $arguments) public static function __callStatic($name, $arguments)
{ {
// If $request is null, it's a request from \Fuel\Core\Request so use it instead // 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); return call_user_func_array(array("Fuel\Core\Input", $name), $arguments);
} }
Expand Down
7 changes: 4 additions & 3 deletions classes/restserver.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@


use \Config; use \Config;
use \Format; use \Format;
use \Input;
use \FuelException; use \FuelException;
use \stdClass; use \stdClass;


Expand Down Expand Up @@ -109,11 +110,11 @@ public static function __callStatic($method, array $arguments)
*/ */
public static function is_rest() public static function is_rest()
{ {
$pattern = static::$pattern; $pattern = static::$pattern;
$resource = \Request::active()->action; $extension = Input::extension();


// Check if a file extension is used // 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();
} }


/** /**
Expand Down

0 comments on commit dc36463

Please sign in to comment.