Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With the following code you can load the controller classes and execute the methods.</p> <p>This code was written for codeigniter 2.1</p> <p>First add a new file <code>MY_Loader.php</code> in your application/core directory. Add the following code to your newly created <code>MY_Loader.php</code> file:</p> <pre><code>&lt;?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-&gt;_my_controller_paths = array(APPPATH); } public function controller($controller, $name = '', $db_conn = FALSE) { if (is_array($controller)) { foreach ($controller as $babe) { $this-&gt;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-&gt;_my_controllers, TRUE)) { return; } $CI =&amp; get_instance(); if (isset($CI-&gt;$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-&gt;_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-&gt;load-&gt;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-&gt;$name = new $controller(); $this-&gt;_my_controllers[] = $name; return; } // couldn't find the controller show_error('Unable to locate the controller you have specified: '.$controller); } } </code></pre> <p>Now you can load all the controllers in your application/controllers directory. for example:</p> <p>load the controller class Invoice and execute the function test()</p> <pre><code>$this-&gt;load-&gt;controller('invoice','invoice_controller'); $this-&gt;invoice_controller-&gt;test(); </code></pre> <p>or when the class is within a dir</p> <pre><code>$this-&gt;load-&gt;controller('/dir/invoice','invoice_controller'); $this-&gt;invoice_controller-&gt;test(); </code></pre> <p>It just works the same like loading a model</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload