Modules

abstract Controller_REST
extends Kohana_Controller_REST
extends Controller
extends Kohana_Controller

Abstract Controller class for RESTful controller mapping. Supports GET, PUT, POST, and DELETE. By default, these methods will be mapped to these actions:

GET
Mapped to the "index" action, lists all objects
POST
Mapped to the "create" action, creates a new object
PUT
Mapped to the "update" action, update an existing object
DELETE
Mapped to the "delete" action, delete an existing object

Additional methods can be supported by adding the method and action to the $_action_map property.

Using this class within a website will require heavy modification, due to most web browsers only supporting the GET and POST methods. Generally, this class should only be used for web services and APIs.

package
Kohana
category
Controller
author
Kohana Team
copyright
© 2009-2012 Kohana Team
license
http://kohanaframework.org/license

Class declared in SYSPATH/classes/controller/rest.php on line 3.

Properties

public Request $request

Request that created the controller

public Response $response

The response that will be returned from controller

protected array $_action_map

REST types

protected string $_action_requested

requested action

Methods

public action_invalid( ) (defined in Kohana_Controller_REST)

Sends a 405 "Method Not Allowed" response and a list of allowed actions.

Source Code

public function action_invalid()
{
	// Send the "Method Not Allowed" response
	$this->response->status(405)
		->headers('Allow', implode(', ', array_keys($this->_action_map)));
}

public after( ) (defined in Kohana_Controller_REST)

undocumented function

Source Code

public function after()
{
	if (in_array(Arr::get($_SERVER, 'HTTP_X_HTTP_METHOD_OVERRIDE', $this->request->method()), array(
		HTTP_Request::PUT,
		HTTP_Request::POST,
		HTTP_Request::DELETE)))
	{
		$this->response->headers('cache-control', 'no-cache, no-store, max-age=0, must-revalidate');
	}
}

public before( ) (defined in Kohana_Controller_REST)

Checks the requested method against the available methods. If the method is supported, sets the request action from the map. If not supported, the "invalid" action will be called.

Source Code

public function before()
{
	$this->_action_requested = $this->request->action();

	$method = Arr::get($_SERVER, 'HTTP_X_HTTP_METHOD_OVERRIDE', $this->request->method());

	if ( ! isset($this->_action_map[$method]))
	{
		$this->request->action('invalid');
	}
	else
	{
		$this->request->action($this->_action_map[$method]);
	}

	return parent::before();
}

public __construct( Request $request , Response $response ) (defined in Kohana_Controller)

Creates a new controller instance. Each controller must be constructed with the request object that created it.

Parameters

  • Request $request required - Request that created the controller
  • Response $response required - The request's response

Return Values

  • void

Source Code

public function __construct(Request $request, Response $response)
{
	// Assign the request to the controller
	$this->request = $request;

	// Assign a response to the controller
	$this->response = $response;
}