Skip to content

Instantly share code, notes, and snippets.

@daveWid
Created April 11, 2012 12:55
Show Gist options
  • Save daveWid/2359139 to your computer and use it in GitHub Desktop.
Save daveWid/2359139 to your computer and use it in GitHub Desktop.
Controller for Kohana
<?php defined('SYSPATH') or die('No direct script access.');
/**
* A base Controller class.
*
* @author Dave Widmer <dwidmer@bgsu.edu>
*/
class Controller extends Kohana_Controller
{
/**
* @var \Owl\Layout The layout class
*/
protected $layout;
/**
* @var \Owl\View OR array The \Owl\View or array to be json encoded for ajax requests.
*/
protected $content;
/**
* Initial setup
*/
public function before()
{
$mobile = new Mobile_Detect;
$class = $mobile->isMobile() ? "View_Layout_Mobile" : "View_Layout_Browser";
$this->layout = new $class;
}
/**
* Sends the request to the browser/device.
*/
public function after()
{
if ($this->request->is_ajax())
{
$type = "application/json";
$content = json_encode($this->content);
}
elseif (Arr::get($this->request->query(), "callback", false) !== false)
{
$type = "text/javascript; charset=".Kohana::$charset;
$content = $this->request->query('callback')."(".json_encode($this->content).")";
}
else
{
$type = "text/html";
$this->layout->content($this->content);
$content = $this->layout->render();
}
// Send the proper response
$this->response->headers("Content-Type", $type)->body($content);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment