Skip to content
Derek Jones edited this page Jul 5, 2012 · 7 revisions

Introduction A while ago i was wild about customizing codeigniter.php but it's possible to do this with hooks too and this is where the defaults library comes in

The code

config/defaults.php :

<?php
// set suffixes for the models
$config['default_model_suffix'] = '_model';
$config['default_validation_suffix'] = '_validation';
// set the model functionality
$config['default_do_validation'] = TRUE;
$config['default_set_vars'] = TRUE;
// set master view
$config['default_master_view'] = '';

hooks/defaults.php :

<?php

class Defaults
{
    var $CI;
    var $dir     = '';
    var $class   = '';
    var $method  = '';

    function Defaults()
    {
        // get controller object
        $this->CI =& get_instance();
        // get router variables
        $RTR =& load_class('Router');
        $this->dir =  $RTR->fetch_directory();
        $this->class = $RTR->fetch_class();
        $this->method = $RTR->fetch_method();
        // get config file for class
        $this->CI->load->config('defaults');
    }
    /**
    * load model and validation files based on the controller and method
    *
    * @return void
    */
    function model()
    {
       // load default model if it exists
        $defaultmodel = strtolower($this->class).config_item('default_model_suffix');
        $defaultmodelfile = $this->dir.$defaultmodel;
       if(file_exists(APPPATH.'models/'.$defaultmodelfile.EXT))
       {
           $this->CI->load->model($defaultmodelfile,'',TRUE);
           // execute validation if it exists and is wanted
           if(config_item('default_do_validation'))
           {
               $defaultvalidation = strtolower($this->class).config_item('default_validation_suffix');
               $defaultvalidationfile = $this->dir.$defaultvalidation;
               if(file_exists(APPPATH.'models/'.$defaultvalidationfile.EXT))
               {
                   $this->CI->load->model($defaultvalidationfile,'',TRUE);
                   $validationviewmethod = strtolower($this->method);
                      
                   if(method_exists($this->CI->$defaultvalidation, $validationviewmethod) === TRUE)
                   {
                       $this->CI->$defaultvalidation->$validationviewmethod();
                   }
               }
           }
           // load view file vars from the model if wanted
           if(config_item('default_set_vars'))
           {
                $modelviewmethod = strtolower($this->method);
                if(method_exists($this->CI->$defaultmodel,$modelviewmethod) === TRUE)
                {
                     $this->CI->load->vars( $this->CI->$defaultmodel->$modelviewmethod() );
                }
           }
       }
    }
    /**
    * load view file based on controller and method
    *
    * @return void
    */
    function view()
    {
        
        $defaultview = (config_item('default_master_view') != '') ? config_item('default_master_view') : $this->dir.strtolower($this->class.'/'.$this->method);
        if(file_exists(APPPATH.'views/'.$defaultview.EXT))
        {
            $this->CI->load->view($defaultview);
        }
    }
}

Installation _ config/config.php setting enable_hooks to true _ config/hooks.php

$hook['post_controller_constructor'] = array('class'    => 'Defaults',
                                'function' => 'model',
                                'filename' => 'defaults.php',
                                'filepath' => 'hooks');

$hook['post_controller'] = array('class'    => 'Defaults',
                                'function' => 'view',
                                'filename' => 'defaults.php',
                                'filepath' => 'hooks');

Usage If you use the config file as it is you can create following file structure

application

  • controllers -- test.php
  • models -- test_model.php -- test_validation.php
  • views -- test --- index.php

When requesting the controller test and the method index the test_model.php file gets loaded, the test_validation.php file gets loaded, a method called index gets called in the validation file, a method index gets called in the model file and the return gets stored in an array with view variables. This all happens before the controller method call. After the controller method call the index.php file in the views/test directory is loaded.

Variations on this behavior are; doing the validation yourself, loading the view variables yourself, load a common/master view file instead of a controller-method dependent view file

Category:Contributions::Libraries::Miscallenous

Clone this wiki locally