CodeIgniter: load controller in the controller

I have a home controller with an index action that displays a set of recognized products. However, product management is done using the product controller, including the proprietary model and views.

How do I access product information from an index action in a home controller? Instancing product will not work because the class does not load at run time, and CodeIgniter does not provide the ability to dynamically load controllers. Putting the product class in the library file also does not work.

To be precise, I need product representations (filled with data processed by the product controller) inserted into the index view. I am running CodeIgniter 2.0.2.

+42
php codeigniter
May 22 '11 at 22:00
source share
10 answers

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

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

Modular extensions make the modular structure of CodeIgniter PHP modular. Modules are groups of independent components, typically a model, controller, and view, located in a subdirectory of application modules that can be deleted in other CodeIgniter applications.

OK, so the big change is that now you will use a modular structure, but this is desirable for me. I have been using CI for about 3 years and cannot imagine life without modular extensions.

Now, here is the part that deals with directly calling controllers for rendering partial view:

 // 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. I usually use this to load small “widgets”, for example:

  • calendar of events
  • Latest News List
  • Newsletter Subscription Formats
  • Polls

Usually 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. Hope this helps you, although it may be a little more than what you were looking for. I have been using MX since then and have not looked back.

Be sure to read the docs and see a lot of information about this package on Codeigniter forums. Enjoy it!

+30
May 23 '11 at 1:08 a.m.
source share

Download it like this:

 $this->load->library('../controllers/instructor'); 

and call the following method:

 $this->instructor->functioname() 

This works for CodeIgniter 2.x.

+62
Jan 04 '13 at 15:11
source share

Just add more information about what Zain Abbas said:

Download the controller this way and use it as it said:

 $this->load->library('../controllers/instructor'); $this->instructor->functioname(); 

Or you can create an object and use it as follows:

 $this->load->library('../controllers/your_controller'); $obj = new $this->your_controller(); $obj->your_function(); 

Hope this helps.

+8
Jun 12. '13 at 8:47
source share

In this case, you can try the old school php.

// insert at the beggining of home.php controller require_once(dirname(__FILE__)."/product.php"); // the controller route.

Then you will have something like:

 Class Home extends CI_Controller { public function __construct() { parent::__construct(); $this->product = new Product(); ... } ... // usage example public function addProduct($data) { $this->product->add($data); } } 

And then just use the controller methods as you like.

+7
Dec 01 '11 at 20:51
source share

Based on @Joaquin Astelarra's answer, I managed to write this little helper called load_controller_helper.php :

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); if (!function_exists('load_controller')) { function load_controller($controller, $method = 'index') { require_once(FCPATH . APPPATH . 'controllers/' . $controller . '.php'); $controller = new $controller(); return $controller->$method(); } } 

You can use / call it like this:

 $this->load->helper('load_controller'); load_controller('homepage', 'not_found'); 

Note. The second argument is optional, as it will run a method called index , such as CodeIgniter.

Now you can load the controller inside another controller without using HMVC.

Later Edit: Keep in mind that this method may have unexpected results. Always check it out!

+5
May 03 '13 at 8:20
source share

There are many good answers here for loading controllers inside controllers, but for me this is against the mvc pattern.

I am worried about the sentence:

(filled with data processed by the product controller)

Models for processing and returning data. If you put this logic in your product model, you can call it from any controller that you like without trying to pervert the framework.

One of the most useful quotes I read was that the controller was like a “cop” to route requests and responses between models and views.

+1
Jun 24 '15 at 8:44
source share

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

This code was written for codeigniter 2.1

First add the new file MY_Loader.php to the application / main directory. Add the following code to the just 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 into the application / controller directory. eg:

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

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

or when the class is inside the directory

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

It just works the same as loading a model.

0
Dec 24 '12 at 11:20
source share

Just use

..............

self::index();

..............

0
Dec 19 '13 at 11:21
source share

According to this blog post, you can download the controller in 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 the controller into another controller.

0
Jun 06 '14 at 8:04
source share

I know this is old, but if someone finds it recently, I would suggest creating a separate class file in the controllers folder. Pass the existing controller object to the constructor of the class, and then you can access the functions from anywhere and not conflict with CI configuration and processing.

0
Jun 29 '16 at 14:18
source share



All Articles