блин и никто не написал что сами файлы не выложил =\ завтра подчищу и выложу остальные классы, а пока код AJAX контроллера:
Код:
abstract class Controller_CMS_Ajax extends Controller_Basic
{
/**
* @var boolean View auto render. Not used in AJAX
*/
protected $auto_render = FALSE;
/**
* @var mixed Response data type(text, html, json, xml). If empty not convert data
*/
protected $data_type = 'json';
/**
* Automatically executed before the controller action. Can be used to set
* class properties, do authorization checks, and execute other custom code.
*
* @return void
*/
public function before()
{
// Check request type
if ( ! $this->request->is_ajax())
{
throw HTTP_Exception::factory(501, 'AJAX request not detected');
// throw new Request_Exception('AJAX request not detected', 501);
}
// Set response data type
$this->data_type = $this->param('data_type', $this->data_type);
if ( ! empty($this->data_type) AND ! in_array($this->data_type, array('text', 'html', 'json', 'xml')))
{
throw HTTP_Exception::factory(500, 'Wrong response data type');
// throw new Request_Exception('Wrong response data type', 500);
}
parent::before();
}
/**
* Automatically executed after the controller action. Can be used to apply
* transformation to the response, add extra output, and execute other custom code.
*
* @return void
*/
public function after()
{
// Convert response data
if ( ! empty($this->data_type))
{
$this->view = CMS::convert($this->view, $this->data_type);
}
// Add special header fields for AJAX requests
$this->headers = array_merge(
$this->headers,
array(
'Content-Type' => 'text/'.$this->data_type,
'Pragma' => 'no-cache',
'Cache-Control' => 'no-store, no-cache, must-revalidate',
)
);
parent::after();
}
} // End Controller_Ajax
Основной момент в нем это авто конвертация результата с помощью метода CMS::convert (первый параметр - исходные данные, второй - тип данных). Его код выложу позднее вместе с описанием всего хэлпера CMS, вместо этого метода можно использовать модуль dataflow:
https://github.com/morgan/kohana-dataflow . Результат работы action'а (response body)сохраняется в $this->view по аналогии с остальными контроллерами. Дополнительные заголовки заголовки служат для указания типа данных и отключения кеширования AJAX запросов.
Если что-то не понятно, пишите - сделаю примеры.