Skip to content

Commit

Permalink
Merge pull request #2 from eric-famiglietti/master
Browse files Browse the repository at this point in the history
Add helper loading.
  • Loading branch information
jamierumbelow committed Sep 12, 2012
2 parents 1ecc761 + 9163ea3 commit 5f99e73
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 27 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ codeigniter-base-controller is an extended `CI_Controller` class to use in your
{
protected $models = array( 'user', 'group' );

protected $helpers = array( 'cookie', 'file' );

public function index()
{
$this->data['users'] = $this->user->get_all();
Expand Down Expand Up @@ -112,6 +114,12 @@ If, for example, you name your models `model_user`, you can specify the model st
$this->user->get(1);
}

## Helper Loading

You can specify a lost of helpers to load with the `$this->helpers` variable:

protected $helpers = array( 'cookie', 'file', 'xml' );

## Per-controller 404 override

Before CodeIgniter throws a standard 404, `MY_Controller` will look for a `_404` method on the controller. This allows you to customise the output of the 404 page on a controller-by-controller basis.
Expand All @@ -121,6 +129,7 @@ Before CodeIgniter throws a standard 404, `MY_Controller` will look for a `_404`
**Version 1.3.0 - IN DEVELOPMENT**
* Vastly improved documentation
* Added unit test suite
* Added helper autoloading

**Version 1.0.0 - 1.2.0**
* Initial Release
63 changes: 42 additions & 21 deletions core/MY_Controller.php
Original file line number Diff line number Diff line change
@@ -1,72 +1,78 @@
<?php
/**
* A base controller for CodeIgniter with view autoloading, layout support,
* model loading, asides/partials and per-controller 404
* model loading, helper loading, asides/partials and per-controller 404
*
* @link http://github.com/jamierumbelow/codeigniter-base-controller
* @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net>
*/

class MY_Controller extends CI_Controller
{
{

/* --------------------------------------------------------------
* VARIABLES
* ------------------------------------------------------------ */

/**
* The current request's view. Automatically guessed
* The current request's view. Automatically guessed
* from the name of the controller and action
*/
protected $view = '';

/**
* An array of variables to be passed through to the
* An array of variables to be passed through to the
* view, layout and any asides
*/
protected $data = array();

/**
* The name of the layout to wrap around the view.
*/
protected $layout;

/**
* An arbitrary list of asides/partials to be loaded into
* the layout. The key is the declared name, the value the file
*/
protected $asides = array();

/**
* A list of models to be autoloaded
*/
protected $models = array();

/**
* A formatting string for the model autoloading feature.
* The percent symbol (%) will be replaced with the model name.
*/
protected $model_string = '%_model';


/**
* A list of helpers to be autoloaded
*/
protected $helpers = array();

/* --------------------------------------------------------------
* GENERIC METHODS
* ------------------------------------------------------------ */

/**
* Initialise the controller, tie into the CodeIgniter superobject
* and try to autoload the models
* and try to autoload the models and helpers
*/
public function __construct()
{
parent::__construct();

$this->_load_models();
$this->_load_helpers();
}

/* --------------------------------------------------------------
* VIEW RENDERING
* ------------------------------------------------------------ */

/**
* Override CodeIgniter's despatch mechanism and route the request
* through to the appropriate action. Support custom 404 methods and
Expand All @@ -89,10 +95,10 @@ public function _remap($method)
show_404(strtolower(get_class($this)).'/'.$method);
}
}

$this->_load_view();
}

/**
* Automatically load the view, allowing the developer to override if
* he or she wishes, otherwise being conventional.
Expand All @@ -104,10 +110,10 @@ protected function _load_view()
{
// If $this->view isn't empty, load it. If it isn't, try and guess based on the controller and action name
$view = (!empty($this->view)) ? $this->view : $this->router->directory . $this->router->class . '/' . $this->router->method;

// Load the view into $yield
$data['yield'] = $this->load->view($view, $this->data, TRUE);

// Do we have any asides? Load them.
if (!empty($this->asides))
{
Expand All @@ -116,7 +122,7 @@ protected function _load_view()
$data['yield_'.$name] = $this->load->view($file, $this->data, TRUE);
}
}

// Load in our existing data with the asides and view
$data = array_merge($this->data, $data);
$layout = FALSE;
Expand All @@ -127,7 +133,7 @@ protected function _load_view()
if (file_exists(APPPATH . 'views/layouts/' . $this->router->class . '.php'))
{
$layout = 'layouts/' . $this->router->class;
}
}
else
{
$layout = 'layouts/application';
Expand Down Expand Up @@ -157,7 +163,7 @@ protected function _load_view()
/* --------------------------------------------------------------
* MODEL LOADING
* ------------------------------------------------------------ */

/**
* Load models based on the $this->models array
*/
Expand All @@ -168,7 +174,7 @@ private function _load_models()
$this->load->model($this->_model_name($model), $model);
}
}

/**
* Returns the loadable model name based on
* the model formatting string
Expand All @@ -177,4 +183,19 @@ protected function _model_name($model)
{
return str_replace('%', $model, $this->model_string);
}

/* --------------------------------------------------------------
* HELPER LOADING
* ------------------------------------------------------------ */

/**
* Load helpers based on the $this->helpers array
*/
private function _load_helpers()
{
foreach ($this->helpers as $helper)
{
$this->load->helper($helper);
}
}
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
<phpunit
colors="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true">
Expand Down
26 changes: 24 additions & 2 deletions tests/MY_Controller_tests.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* A base controller for CodeIgniter with view autoloading, layout support,
* model loading, asides/partials and per-controller 404
* model loading, helper loading, asides/partials and per-controller 404
*
* @link http://github.com/jamierumbelow/codeigniter-base-controller
* @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net>
Expand All @@ -16,7 +16,7 @@ class MY_Controller_tests extends PHPUnit_Framework_TestCase
/* --------------------------------------------------------------
* VIEW AUTOLOADING
* ------------------------------------------------------------ */

public function test_autoloads_view()
{
$this->controller = new Users();
Expand Down Expand Up @@ -145,4 +145,26 @@ public function test_constructor_loads_differently_named_models()

$this->controller->__construct();
}

/* --------------------------------------------------------------
* HELPER LOADING
* ------------------------------------------------------------ */

public function test_constructor_loads_helpers()
{
$this->controller = new Helpers();
$this->controller->load = $this->getMock('CI_Loader');

$this->controller->load->expects($this->at(0))
->method('helper')
->with($this->equalTo('cookie'));
$this->controller->load->expects($this->at(1))
->method('helper')
->with($this->equalTo('file'));
$this->controller->load->expects($this->at(2))
->method('helper')
->with($this->equalTo('xml'));

$this->controller->__construct();
}
}
7 changes: 6 additions & 1 deletion tests/support/test_controllers.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* A base controller for CodeIgniter with view autoloading, layout support,
* model loading, asides/partials and per-controller 404
* model loading, helper loading, asides/partials and per-controller 404
*
* @link http://github.com/jamierumbelow/codeigniter-base-controller
* @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net>
Expand Down Expand Up @@ -52,4 +52,9 @@ class No_Layout extends MY_Controller
protected $layout = FALSE;

public function index() { }
}

class Helpers extends MY_Controller
{
protected $helpers = array( 'cookie', 'file', 'xml' );
}
5 changes: 3 additions & 2 deletions tests/support/test_helper.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* A base controller for CodeIgniter with view autoloading, layout support,
* model loading, asides/partials and per-controller 404
* model loading, helper loading, asides/partials and per-controller 404
*
* @link http://github.com/jamierumbelow/codeigniter-base-controller
* @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net>
Expand All @@ -11,7 +11,7 @@
require_once 'tests/support/test_controllers.php';

/**
* test_helper.php is the bootstrap file for our tests - it loads up an
* test_helper.php is the bootstrap file for our tests - it loads up an
* appropriate faux-CodeIgniter environment for our tests to run in.
*/

Expand All @@ -35,6 +35,7 @@ public function __construct()
class CI_Loader
{
public function model($name, $assign) {}
public function helper($name) {}
public function view($file, $data = array(), $ret = FALSE) {}

public function __call($method, $params = array()) {}
Expand Down

0 comments on commit 5f99e73

Please sign in to comment.