Welcome Guest, Not a member yet? Register   Sign In
Trying to extend Loader to call app controllers
#1

[eluser]Stuart Marsh[/eluser]
Hi everyone.
I'm fairly new to CodeIgniter but I am already impressed by how flexible and easy it is.
However like many people on here I am disappointed that I cannot call controllers within controllers. I know it goes against the MVC structure and that helpers can be created but this would be a much more flexible way to do things.
So I have started to play with CodeIgniter and I'm trying to extend the Loader library so I can call other controllers. I have a library in my app called MY_Loader.php and this is what I have put in it:
Code:
class MY_Loader extends CI_Loader {
    
    /**
     * Constructor
     *
     * @access    public
     */
    function MY_Loader()
    {
        parent::CI_Loader();
    }
    
    /**
     * Controller Class Loader
     *
     * This function lets users load and instantiate controller classes.
     * It is designed to be called from a user's app controllers.
     *
     * @access    public
     * @param    string    the name of the class
     * @param    mixed    the optional parameters
     * @return    void
     */    
    function Controller($library = '', $params = NULL)
    {        
        if ($library == '')
        {
            return FALSE;
        }

        if (is_array($library))
        {
            foreach ($library as $class)
            {
                $this->_ci_load_controller_class($class, $params);
            }
        }
        else
        {
            $this->_ci_load_controller_class($library, $params);
        }
        
        $this->_ci_assign_to_models();
    }

    // --------------------------------------------------------------------
    
    /**
     * Load Controller class
     *
     * This function loads the requested controller class.
     *
     * @access    private
     * @param     string    the item that is being loaded
     * @param    mixed    any additional parameters
     * @return     void
     */
    function _ci_load_controller_class($class, $params = NULL)
    {    
        // Get the class name
        $class = str_replace(EXT, '', $class);

        // We'll test for both lowercase and capitalized versions of the file name
        foreach (array(ucfirst($class), strtolower($class)) as $class)
        {            
            // Lets search for the requested library file and load it.
            $is_duplicate = FALSE;
            for ($i = 1; $i < 3; $i++)
            {
                $path = ($i % 2) ? APPPATH : BASEPATH;    
                $fp = APPPATH.'controllers/'.$class.EXT;
                
                // Does the file exist?  No?  Bummer...
                if ( ! file_exists($fp))
                {
                    continue;
                }
                
                // Safety:  Was the class already loaded by a previous call?
                if (in_array($fp, $this->_ci_classes))
                {
                    $is_duplicate = TRUE;
                    log_message('debug', $class." class already loaded. Second attempt ignored.");
                    return;
                }
                
                include($fp);
                $this->_ci_classes[] = $fp;
                return $this->_ci_init_class($class, '', $params);
            }
        } // END FOREACH
        
        // If we got this far we were unable to find the requested class.
        // We do not issue errors if the load call failed due to a duplicate request
        if ($is_duplicate == FALSE)
        {
            log_message('error', "Unable to load the requested class: ".$class);
            show_error("Unable to load the requested class: ".$class);
        }
    }
Basically it is a copy of Library and _ci_load_class but with the controller directory. This means controllers I can call the controller with $this->load->Controller('Controller_filename') then $this->controller_name->function(). However, when these 'sub' controllers try to use models or helpers they fail.
For example, If the controller I access uses a model I get the following message.
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Form_Model::$db

Filename: models/form_model.php

Line Number: 21

Could anybody help me get this working or help me understand why it is failing?


Messages In This Thread
Trying to extend Loader to call app controllers - by El Forum - 06-28-2007, 04:21 AM
Trying to extend Loader to call app controllers - by El Forum - 06-28-2007, 04:42 AM
Trying to extend Loader to call app controllers - by El Forum - 06-28-2007, 05:44 AM
Trying to extend Loader to call app controllers - by El Forum - 06-28-2007, 06:25 AM
Trying to extend Loader to call app controllers - by El Forum - 06-28-2007, 06:52 AM
Trying to extend Loader to call app controllers - by El Forum - 06-28-2007, 07:01 AM
Trying to extend Loader to call app controllers - by El Forum - 06-28-2007, 07:20 AM
Trying to extend Loader to call app controllers - by El Forum - 06-28-2007, 10:14 AM
Trying to extend Loader to call app controllers - by El Forum - 06-28-2007, 10:59 AM
Trying to extend Loader to call app controllers - by El Forum - 06-28-2007, 06:18 PM
Trying to extend Loader to call app controllers - by El Forum - 06-28-2007, 09:16 PM
Trying to extend Loader to call app controllers - by El Forum - 06-29-2007, 07:45 PM



Theme © iAndrew 2016 - Forum software by © MyBB