Codeigniter Call Controller From Controller

后端 未结 3 2074
清酒与你
清酒与你 2021-01-05 02:16

After the last two comments, I\'ll dump out my real code and maybe it will help out:

Here is the landing Controller:

 

        
相关标签:
3条回答
  • 2021-01-05 02:30

    So after lots of searching, I think I have a workaround. The issue what what I thought with the instance. After diving into the framework I realized that it is storing the instance into as static var, private static $instance. I modified the constructor to not overwrite if that var has been populated. On top of that, since there were some oddities still with the loading, for some reason objects would be marked as loaded but in reality were not, I had to add a new var to the controller, protected $ci_instance. In the end, I modified the CI_Controller to look like the following:

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    /**
     * CodeIgniter
     *
     * An open source application development framework for PHP 5.1.6 or newer
     *
     * @package     CodeIgniter
     * @author      ExpressionEngine Dev Team
     * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
     * @license     http://codeigniter.com/user_guide/license.html
     * @link        http://codeigniter.com
     * @since       Version 1.0
     * @filesource
     */
    
    // ------------------------------------------------------------------------
    
    /**
     * CodeIgniter Application Controller Class
     *
     * This class object is the super class that every library in
     * CodeIgniter will be assigned to.
     *
     * @package     CodeIgniter
     * @subpackage  Libraries
     * @category    Libraries
     * @author      ExpressionEngine Dev Team
     * @link        http://codeigniter.com/user_guide/general/controllers.html
     */
    class CI_Controller {
    
        private static $instance;
        protected $ci_instance; // line added
    
        /**
         * Constructor
         */
        public function __construct()
        {
    
            if(self::$instance == null) // line added
                self::$instance =& $this;
    
            $this->ci_instance =& get_instance(); // line added
    
            // Assign all the class objects that were instantiated by the
            // bootstrap file (CodeIgniter.php) to local class variables
            // so that CI can run as one big super object.
            foreach (is_loaded() as $var => $class)
            {
                $this->$var =& load_class($class);
            }
    
            $this->load =& load_class('Loader', 'core');
    
            $this->load->_base_classes =& is_loaded();
    
            $this->load->_ci_autoloader();
    
            log_message('debug', "Controller Class Initialized");
        }
    
        public static function &get_instance()
        {
            return self::$instance;
        }
    }
    // END Controller class
    
    /* End of file Controller.php */
    /* Location: ./system/core/Controller.php */
    

    The only issue so far is that I cannot do $this->load->model("some_model");. Instead I have to use $this->ci_instance->load->model("some_model"); and everything will stem from there. I don't really care about the extra var, but what I don't like is modifying out of box solutions because it increases the complexity to do an upgrade.

    for now I've marked this as an answer because it is what "I" have chosen to use as my solution, but I am still opened to a better solution than the one I am using. An exact description of what needs to be solved is as follows:

    Copy all loaded properties from one instance to another. Basically do a merger of two instances if possible.

    If someone can answer that with a better solution than mine, preferably without modifying the codeigniter core, I'd gladly change my answer because I am not happy with my solution because I don't know what effects I might encounter later on during development.

    0 讨论(0)
  • 2021-01-05 02:35

    In your application/autoload.php set codeigniter to load database class.

    $autoload['libraries'] = array('database', 'otherlibrary', 'otherlibrary2');
    

    It must be all you need to solve your problem.

    0 讨论(0)
  • 2021-01-05 02:41

    if u using HMVC just using

    Class Models extends MX_Loader{
    
       function getUser($username){
           $sql="SELECT
                       *
                  FROM
                       user
                  WHERE username = ? "
           return $this->db->query($sql,array($username))->row();
       }
    }
    
    0 讨论(0)
提交回复
热议问题