Как сказано выше, вам не нужен отдельный контроллер для ваших действий ajax. Вы можете воспользоваться объектом запроса Kohana для определения типа запроса. Это можно сделать следующим образом:
<?php
class Controller_Test extends Controller_Template {
/**
* @var View Template container
*/
protected $template = 'template';
/**
* @var View Content to render
*/
protected $content = 'some/content/view';
// Inherited from parent class
protected $auto_template_render = TRUE;
public function before()
{
parent::before();
if ($this->request->is_ajax() OR !$this->request->is_initial()) {
$this->auto_template_render = FALSE;
}
}
public function after()
{
if ($this->auto_template_render == FALSE) {
// We have ajax or internal request here
$this->template = $this->content;
} else {
// We have regular http request for a page
$this->template = View::factory($this->template)
->set('content', $this->content);
}
// Call parent method
parent::after();
}
}
Хотя пример очень прост, он может быть улучшен до того, что вы хотите заархивировать. По сути, я закончил писать свой Controller_Template
, чтобы делать то, что мне нужно. Также вы можете рассмотреть возможность добавления параметра формата к вашим URL-адресам, чтобы URL-адреса .html
возвращали обычное html-представление данных, а URL-адреса .json
делали то же самое, но в формате json.
Для получения дополнительной информации (и, возможно, идеи) см. Kerkness неофициальные вики Kohana