Welcome Guest, Not a member yet? Register   Sign In
Very Basic Module Library
#1

[eluser]affix[/eluser]
I just coded a basic module library for CI. You need the following config variables

Code:
$config['module_dir'] = "../../modules"; //Modules will be stored outside the system dir

The Library

Code:
<?php
/**
* Codeigniter Module Library
*
* @package Module
* @category Libraries
* @author Keiran Smith
* @link http://Keiran-Smith.net
* @version 1.0
* @copyright Keiran Smith 2010
*
* This Library provides Module Functionality to your CI Project
* This allows you to create modules for your system without needing
* to modify your base code!
*/

class Module
{
    var $CI =& get_instance(); // Load the Codeigniter Instance
    var $config = $config; // Load the Codeigniter configuration
    
    function Module()
    {
        
    }
    
    /*
    *  Function : load
    *  module : string [required]
    *
    *  Loads the module into you CI instance
    *  @access : public
    *  @param : string
    */
    function load($module)
    {
        $this->CI->load($this->config['module_dir'] . $module . "/" . $module . ".php");
    }
    
    /*
    *  Function : get_module_info
    *  module : string [required]
    *  Parses the INI File for the module
    *
    *  @access : public
    *  @param : string
    */
    function get_module_info($module)
    {
        $ini_array = parse_ini_file($this->config['module_dir'] . $module . "/" . $module . ".ini", true);
        return $ini_array;
    }
    
    /*
    *  Function : scan_module_dir
    *  Scans the module directory for valid modules
    *  Returns an Array of modules in the module dir
    *  also returns module information
    *
    *  Return Sample :
    *    $module = array( 'modFile' => "forum/forum.php",
    *                     'modConfig' => array(
    *                                             'Author' => "Keiran Smith",
    *                                             'Homepage' => "http://keiran-smith.net",
    *                                             'ModName'  => "Forum Sample Module",
    *                                             'ModVer' => "1.0"))
    *
    *  @access : public
    *  @param : none
    */
    function scan_module_dir()
    {
        $dh  = opendir($this->config['module_dir']);
        while (false !== ($filename = readdir($dh))) {
            $files[] = $filename;
        }
        
        $module = array();
        foreach($files as $file)
        {
            if(is_dir($file))
            {
                if(file_exists($file . "/$file.php"))
                {
                    $ini_file = $this->get_module_info();
                    $modConfig = array(
                                        'Author' => $ini_file['Author'],
                                        'Homepage' => ini_file['Homepage'],
                                        'ModName' => ini_file['Module_Name'],
                                        'ModVer' => $ini_file['Module_Version']
                                        );
                    
                    $module[] = array(
                                        'modFile' => "$file/$file.php",
                                        'modConfig' => $modConfig
                                      );
                }
                else
                {
                    log("$file is not a valid module!");
                }
            }
        }
        
        return $module;
    }
    
    /*
    *  function : view
    *  This function will load views required for the module to work
    *  views must be stored in module_dir/views
    *
    *  @access : public
    *  @param : string
    */
    function view($page, $params = NULL)
    {
        if($params !== NULL)
        {
            $data['data'] = $params;
        }
        
        $this->CI->load->view($this->config['module_dir'].'views/$page.php', $data);
    }
}

?>

Comments Welcome. For support E-Mail : [email protected]
#2

[eluser]wiredesignz[/eluser]
Have you even tested this?
This code is invalid and will cause an error.
Code:
var $CI =& get_instance();
#3

[eluser]Phil Sturgeon[/eluser]
Agreed. Also the following will give you an error as thee is no load() method in the Loader:

Code:
$this->CI->load(




Theme © iAndrew 2016 - Forum software by © MyBB