Created
December 23, 2010 11:53
-
-
Save lukemorton/752877 to your computer and use it in GitHub Desktop.
This file contains 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.'); | |
/** | |
* Controller default view loading for Kostache! | |
* | |
* To get your controllers guessing what views to load simply | |
* place this file into your "classes" directory in your application. | |
*/ | |
class Controller extends Kohana_Controller { | |
/** | |
* @var mixed Holds the current view instance | |
*/ | |
public $view = NULL; | |
/** | |
* @var string The view class | |
*/ | |
public $view_class = 'View'; | |
/** | |
* @var array Define viewless actions here | |
*/ | |
public $viewless = array(); | |
/** | |
* The before method can be used for predicting a view. It uses the | |
* controller and it's directory, along with the action to work out | |
* what path to load. For example Controller_Blog_Comments::action_add | |
* would have a view location of blog/comments/add. | |
* | |
* @return void | |
*/ | |
public function before() | |
{ | |
// If not a viewless action | |
if ( ! in_array($this->request->action, $this->viewless)) | |
{ | |
$view_parts = array(); | |
foreach (array('directory', 'controller', 'action') as $_part) | |
{ | |
if (isset($this->request->{$_part})) | |
{ | |
$view_parts[] = $this->request->{$_part}; | |
} | |
} | |
// Build the view location | |
$view_location = implode('/', $view_parts); | |
// Load the view using chosen class | |
$this->view = call_user_func($this->view_class.'::factory', $view_location); | |
} | |
return parent::before(); | |
} | |
/** | |
* The after method turns the view into a response | |
* | |
* @return void | |
*/ | |
public function after() | |
{ | |
if ($this->view !== NULL) | |
{ | |
$this->request->response = $this->view; | |
} | |
return parent::after(); | |
} | |
} |
This file contains 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.'); | |
/** | |
* Example controller using the view guessing marlarkey | |
*/ | |
class Controller_Blog extends Controller { | |
/** | |
* Let's use Kostache instead! | |
* @var | |
* @see Controller::$view_class | |
*/ | |
public $view_class = 'Kostache'; | |
/** | |
* @var | |
* @see Controller::$viewless | |
*/ | |
public $viewless = array('create'); | |
/** | |
* For this action the view "blog/index" will have been already loaded. | |
* | |
* @return void | |
*/ | |
public function action_index() | |
{ | |
// Set some $_GET data to view | |
$this->view->set(Arr::extract($_GET, array('page', 'order_by'))); | |
} | |
/** | |
* This action loads the "blog/add" view and nothing else | |
* | |
* @return void | |
*/ | |
public function action_add() | |
{ | |
// No need to do anything | |
} | |
/** | |
* This action creates the blog post, it doesn't need a view. | |
* | |
* @return void | |
*/ | |
public function action_create() | |
{ | |
$post = Model::factory('post', $_POST); | |
// Try and create | |
try | |
{ | |
$post->create(); | |
} | |
catch (Kohana_Validate_Exception $e) | |
{ | |
// We set the errors to session here before we redirect. | |
Session::instance()->set('errors', $e->array->errors()); | |
} | |
// Redirect back to index action | |
$this->request->redirect($this->request->uri(array('action' => 'add'))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment