CodeIgniter: Load controller within controller

后端 未结 10 1698
余生分开走
余生分开走 2020-11-28 07:43

I have a home controller with an index action that displays a set of featured products. However, the products are managed through a product

相关标签:
10条回答
  • 2020-11-28 08:39

    I know this is old, but should anyone find it more recently, I would suggest creating a separate class file in the controllers folder. Pass in the existing controller object into the class constructor and then you can access the functions from anywhere and it doesn't conflict with CI's setup and handling.

    0 讨论(0)
  • 2020-11-28 08:42

    If you're interested, there's a well-established package out there that you can add to your Codeigniter project that will handle this:

    https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/

    Modular Extensions makes the CodeIgniter PHP framework modular. Modules are groups of independent components, typically model, controller and view, arranged in an application modules sub-directory, that can be dropped into other CodeIgniter applications.

    OK, so the big change is that now you'd be using a modular structure - but to me this is desirable. I have used CI for about 3 years now, and can't imagine life without Modular Extensions.

    Now, here's the part that deals with directly calling controllers for rendering view partials:

    // Using a Module as a view partial from within a view is as easy as writing:
    <?php echo modules::run('module/controller/method', $param1, $params2); ?>
    

    That's all there is to it. I typically use this for loading little "widgets" like:

    • Event calendars
    • List of latest news articles
    • Newsletter signup forms
    • Polls

    Typically I build a "widget" controller for each module and use it only for this purpose.

    Your question was also one of my first questions when I started with Codeigniter. I hope this helps you out, even though it may be a bit more than you were looking for. I've been using MX ever since and haven't looked back.

    Make sure to read the docs and check out the multitude of information regarding this package on the Codeigniter forums. Enjoy!

    0 讨论(0)
  • 2020-11-28 08:43

    According to this blog post you can load controller within another controller in codeigniter.

    http://www.techsirius.com/2013/01/load-controller-within-another.html

    First of all you need to extend CI_Loader

    <?php
    
    class MY_Loader extends CI_Loader {
    
        public function __construct() {
            parent::__construct();
        }
    
        public function controller($file_name) {
            $CI = & get_instance();
            $file_path = APPPATH.'controllers/' . $file_name . '.php';
            $object_name = $file_name;
            $class_name = ucfirst($file_name);
    
            if (file_exists($file_path)) {
                require $file_path;
    
                $CI->$object_name = new $class_name();
            }
            else {
                show_error('Unable to load the requested controller class: ' . $class_name);
            }
        }
    }
    

    then load controller within another controller.

    0 讨论(0)
  • 2020-11-28 08:45

    With the following code you can load the controller classes and execute the methods.

    This code was written for codeigniter 2.1

    First add a new file MY_Loader.php in your application/core directory. Add the following code to your newly created MY_Loader.php file:

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    // written by AJ  sirderno@yahoo.com
    
    class MY_Loader extends CI_Loader 
    {
        protected $_my_controller_paths     = array();  
    
        protected $_my_controllers          = array();
    
    
        public function __construct()
        {
            parent::__construct();
    
            $this->_my_controller_paths = array(APPPATH);
        }
    
        public function controller($controller, $name = '', $db_conn = FALSE)
        {
            if (is_array($controller))
            {
                foreach ($controller as $babe)
                {
                    $this->controller($babe);
                }
                return;
            }
    
            if ($controller == '')
            {
                return;
            }
    
            $path = '';
    
            // Is the controller in a sub-folder? If so, parse out the filename and path.
            if (($last_slash = strrpos($controller, '/')) !== FALSE)
            {
                // The path is in front of the last slash
                $path = substr($controller, 0, $last_slash + 1);
    
                // And the controller name behind it
                $controller = substr($controller, $last_slash + 1);
            }
    
            if ($name == '')
            {
                $name = $controller;
            }
    
            if (in_array($name, $this->_my_controllers, TRUE))
            {
                return;
            }
    
            $CI =& get_instance();
            if (isset($CI->$name))
            {
                show_error('The controller name you are loading is the name of a resource that is already being used: '.$name);
            }
    
            $controller = strtolower($controller);
    
            foreach ($this->_my_controller_paths as $mod_path)
            {
                if ( ! file_exists($mod_path.'controllers/'.$path.$controller.'.php'))
                {
                    continue;
                }
    
                if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
                {
                    if ($db_conn === TRUE)
                    {
                        $db_conn = '';
                    }
    
                    $CI->load->database($db_conn, FALSE, TRUE);
                }
    
                if ( ! class_exists('CI_Controller'))
                {
                    load_class('Controller', 'core');
                }
    
                require_once($mod_path.'controllers/'.$path.$controller.'.php');
    
                $controller = ucfirst($controller);
    
                $CI->$name = new $controller();
    
                $this->_my_controllers[] = $name;
                return;
            }
    
            // couldn't find the controller
            show_error('Unable to locate the controller you have specified: '.$controller);
        }
    
    }
    

    Now you can load all the controllers in your application/controllers directory. for example:

    load the controller class Invoice and execute the function test()

    $this->load->controller('invoice','invoice_controller');
    
    $this->invoice_controller->test();
    

    or when the class is within a dir

    $this->load->controller('/dir/invoice','invoice_controller');
    
    $this->invoice_controller->test();
    

    It just works the same like loading a model

    0 讨论(0)
提交回复
热议问题