Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nexeck/1124749 to your computer and use it in GitHub Desktop.
Save nexeck/1124749 to your computer and use it in GitHub Desktop.
Ajax Template Controller for Kohana 3.1
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* Ajax Template Controller template
*
* @author Sergei Gladkovskiy <smgladkovskiy@gmail.com>
*/
abstract class Controller_Ajax_Template extends Controller {
protected $_auth_required = FALSE;
protected $_user;
public function before()
{
if($this->request->method() === HTTP_Request::POST and $this->request->post('is_ajax') === 'true')
$this->request->requested_with('xmlhttprequest');
parent::before();
// Ajax Request check
if( ! $this->request->is_ajax() AND Kohana::$environment == Kohana::PRODUCTION)
{
throw new HTTP_Exception_403('non-ajax request!');
}
// Auth requirement
if ($this->_auth_required AND ! Auth::instance()->logged_in())
{
throw new HTTP_Exception_401('Auth required!');
}
elseif(($this->_auth_required AND Auth::instance()->logged_in()) OR Auth::instance()->logged_in())
{
$this->_user = Auth::instance()->get_user();
$this->_is_admin = $this->_user->has_role('admin');
}
$this->response->headers('Content-Type', 'application/json; charset=utf-8');
}
} // End Controller_Ajax_Template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment