Created
April 11, 2012 12:55
-
-
Save daveWid/2359139 to your computer and use it in GitHub Desktop.
Controller for Kohana
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 defined('SYSPATH') or die('No direct script access.'); | |
/** | |
* A base Controller class. | |
* | |
* @author Dave Widmer <[email protected]> | |
*/ | |
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