From 7198fe013d1ce7878ccf0f0c45b5d7cc949e5fc8 Mon Sep 17 00:00:00 2001 From: Jacob Kelley Date: Fri, 18 Jan 2013 19:44:07 -0800 Subject: [PATCH 1/3] Added Autoloader Function --- system/controller.php | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/system/controller.php b/system/controller.php index dc51490..02bcbe5 100644 --- a/system/controller.php +++ b/system/controller.php @@ -2,6 +2,38 @@ class Controller { + function __construct() + { + $this->autoload(); + } + + private function autoload() + { + global $config; + + foreach( $config['autoload'] as $type => $payload ) + { + $funcName = 'load' . ucfirst( substr($type, 0, -1) ); + + if( is_array($payload) ) + { + foreach($payload as $toLoad) + { + if(method_exists($this,$funcName)) + { + if( $type == 'helpers' ) + { + $this->$toLoad = call_user_func(array($this, $funcName), $toLoad); + } elseif( $type == 'plugins' ) + { + call_user_func(array($this, $funcName), $toLoad); + } + } + } + } + } + } + public function loadModel($name) { require(APP_DIR .'models/'. strtolower($name) .'.php'); @@ -37,4 +69,4 @@ public function redirect($loc) } -?> \ No newline at end of file +?> From f5e51f810641769a0ef7c005e6ec7268392b0e79 Mon Sep 17 00:00:00 2001 From: Jacob Kelley Date: Fri, 18 Jan 2013 19:44:57 -0800 Subject: [PATCH 2/3] Updated Config for autoloading --- application/config/config.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/application/config/config.php b/application/config/config.php index d34579b..ad21da2 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -10,4 +10,9 @@ $config['db_username'] = ''; // Database username $config['db_password'] = ''; // Database password -?> \ No newline at end of file +$config['autoload'] = array( + 'plugins' => array(), + 'helpers' => array('session_helper', 'url_helper') +); + +?> From f8ded5d2713c812aac627ca321570c3c6d5cf964 Mon Sep 17 00:00:00 2001 From: Jacob Kelley Date: Fri, 18 Jan 2013 19:46:01 -0800 Subject: [PATCH 3/3] Added example for helper functions --- application/controllers/main.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/application/controllers/main.php b/application/controllers/main.php index b1c40ac..4f6d31a 100644 --- a/application/controllers/main.php +++ b/application/controllers/main.php @@ -2,8 +2,15 @@ class Main extends Controller { + function __construct(){ + parent::__construct(); + } + function index() { + + $user = $this->session_helper->get('user'); + $template = $this->loadView('main_view'); $template->render(); }