Created
April 8, 2011 15:32
-
-
Save j0shua/910111 to your computer and use it in GitHub Desktop.
rest ajax city
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Controller_Rest_Ajax_City extends Controller_Rest_Ajax { | |
public function before() { | |
parent::before(); | |
} | |
public function after() { | |
parent::after(); | |
} | |
public function action_index() { | |
// add response | |
$this->response_add('test', '123'); | |
// add notice | |
$this->notice_add('Example notice'); | |
} | |
} | |
class Controller_Rest_Ajax extends Controller_Rest { | |
public function before() { | |
parent::before(); | |
// make sure it's an ajax request | |
if (!request::$is_ajax) { | |
exit('No access'); | |
} | |
} | |
public function after() { | |
// execute destructor | |
parent::after(); | |
} | |
} | |
class Controller_Rest extends Controller_Json { | |
/** | |
* Contains responses | |
* | |
* @var array | |
*/ | |
protected $_responses = array(); | |
/** | |
* Contains errors | |
* | |
* @var array | |
*/ | |
protected $_erros = array(); | |
/** | |
* Contains notices | |
* | |
* @var array | |
*/ | |
protected $_notices = array(); | |
/** | |
* Constructor | |
*/ | |
public function before() { | |
// execute parent constructor | |
parent::before(); | |
} | |
/** | |
* Destructor | |
*/ | |
public function after() { | |
// hand response over to kohana request handler | |
$this->request->response = $this->compile(); | |
// execute destructor | |
parent::after(); | |
} | |
/** | |
* Compiles response array | |
* | |
* @return array | |
*/ | |
protected function compile() { | |
// init reponse array | |
$response = array(); | |
// add convenience variables | |
$response['has_responses'] = $this->has_responses(); | |
$response['has_errors'] = $this->has_errors(); | |
$response['has_notices'] = $this->has_notices(); | |
// init main categories | |
$response['responses'] = array(); | |
$response['errors'] = array(); | |
$response['notices'] = array(); | |
// add responses if no error occured and responses exist | |
if (!$this->has_errors() && $this->has_responses()) { | |
foreach ($this->_responses as $resp) { | |
$response['responses'][$resp['id']] = $resp['value']; | |
} | |
} | |
// add errors to response array | |
if ($this->has_errors()) { | |
foreach ($this->_erros as $error) { | |
$response['errors'][] = $error; | |
} | |
} | |
// add notices to response array | |
if ($this->has_notices()) { | |
foreach ($this->_notices as $notice) { | |
$response['notices'][] = $notice; | |
} | |
} | |
return $response; | |
} | |
/** | |
* Adds error message to reponse | |
* | |
* @param mixed $message | |
*/ | |
protected function error_add($message) { | |
$this->_erros[] = $message; | |
} | |
/** | |
* Adds response to response | |
* | |
* @param string $id | |
* @param mixed $value | |
*/ | |
protected function response_add($id, $value) { | |
$index = count($this->_responses); | |
$this->_responses[$index]['id'] = $id; | |
$this->_responses[$index]['value'] = $value; | |
} | |
/** | |
* Add notice to reponse | |
* | |
* @param mixed $message | |
*/ | |
protected function notice_add($message) { | |
$this->_notices[] = $message; | |
} | |
/** | |
* Checks if reponse array contains responses | |
* | |
* @return boolean | |
*/ | |
protected function has_responses() { | |
return!empty($this->_responses); | |
} | |
/** | |
* Checks if reponse array contains errors | |
* | |
* @return boolean | |
*/ | |
protected function has_errors() { | |
return!empty($this->_erros); | |
} | |
/** | |
* Checks if reponse array contains notices | |
* | |
* @return boolean | |
*/ | |
protected function has_notices() { | |
return!empty($this->_notices); | |
} | |
} | |
class Controller_Json extends Kohana_Controller_REST { | |
public function before() { | |
// execute constructor | |
parent::before(); | |
// disable browser cache | |
$this->request->headers['Cache-Control'] = 'no-cache, must-revalidate'; | |
$this->request->headers['Content-Type'] = 'application/json'; | |
$this->request->headers['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT'; | |
} | |
public function after() { | |
// json encode response | |
$this->request->response = json_encode($this->request->response); | |
// execute parent destructor | |
parent::after(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment