Last active
September 11, 2023 14:06
-
-
Save lucywoodman/8f71926c3ee27defe7d347ad7ae8ddf2 to your computer and use it in GitHub Desktop.
CodeIgniter v3 list controllers/methods (i.e. auto routes)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') or exit('No direct script access allowed'); | |
/*** | |
* File: (Codeigniterapp)/libraries/Controllerlist.php | |
* | |
* A simple library to list all your controllers with their methods. | |
* This library will return an array with controllers and methods | |
* | |
* The library will scan the "controller" directory and (in case of) one (1) subdirectory level deep | |
* for controllers | |
* | |
* Usage in one of your controllers: | |
* | |
* $this->load->library('controllerlist'); | |
* print_r($this->controllerlist->getControllers()); | |
* | |
* @author Peter Prins | |
*/ | |
class ControllerList | |
{ | |
/** | |
* Codeigniter reference | |
*/ | |
private $CI; | |
private $EXT; | |
/** | |
* Array that will hold the controller names and methods | |
*/ | |
private $aControllers; | |
// Construct | |
function __construct() | |
{ | |
// Get Codeigniter instance | |
$this->CI = get_instance(); | |
$this->CI->EXT = ".php"; | |
// Get all controllers | |
$this->setControllers(); | |
} | |
/** | |
* Return all controllers and their methods | |
* @return array | |
*/ | |
public function getControllers() | |
{ | |
return $this->aControllers; | |
} | |
/** | |
* Set the array holding the controller name and methods | |
*/ | |
public function setControllerMethods($p_sControllerName, $p_aControllerMethods) | |
{ | |
$this->aControllers[$p_sControllerName] = $p_aControllerMethods; | |
} | |
/** | |
* Search and set controller and methods. | |
*/ | |
private function setControllers() | |
{ | |
// Loop through the controller directory | |
foreach (glob(APPPATH . 'controllers/*') as $controller) { | |
// if the value in the loop is a directory loop through that directory | |
if (is_dir($controller)) { | |
// Get name of directory | |
$dirname = basename($controller, $this->CI->EXT); | |
// Loop through the subdirectory | |
if ($dirname !== 'api') { | |
foreach (glob(APPPATH . 'controllers/'.$dirname.'/*') as $subdircontroller) { | |
// Get the name of the subdir | |
$subdircontrollername = basename($subdircontroller, $this->CI->EXT); | |
// Load the controller file in memory if it's not load already | |
if (is_file($subdircontroller)) { | |
if (!class_exists($subdircontrollername)) { | |
$this->CI->load->file($subdircontroller); | |
} | |
} | |
// Add the controllername to the array with its methods | |
$aMethods = get_class_methods($subdircontrollername); | |
$aUserMethods = []; | |
foreach ($aMethods as $method) { | |
if ($method != '__construct' && $method != 'get_instance' && $method != $subdircontrollername) { | |
$aUserMethods[] = $method; | |
} | |
} | |
$this->setControllerMethods($subdircontrollername, $aUserMethods); | |
} | |
} | |
} elseif (pathinfo($controller, PATHINFO_EXTENSION) == "php") { | |
// value is no directory get controller name | |
$controllername = basename($controller, $this->CI->EXT); | |
// Load the class in memory (if it's not loaded already) | |
if (!class_exists($controllername)) { | |
$this->CI->load->file($controller); | |
} | |
// Add controller and methods to the array | |
$aMethods = get_class_methods($controllername); | |
$aUserMethods = []; | |
if (is_array($aMethods)) { | |
foreach ($aMethods as $method) { | |
if ($method != '__construct' && $method != 'get_instance' && $method != $controllername) { | |
$aUserMethods[] = $method; | |
} | |
} | |
} | |
$this->setControllerMethods($controllername, $aUserMethods); | |
} | |
} | |
} | |
} | |
// EOF |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') or exit('No direct script access allowed'); | |
class ListControllers extends CI_controller | |
{ | |
public function __construct() | |
{ | |
parent::__construct(); | |
$this->load->library('controllerlist'); | |
} | |
public function index() | |
{ | |
$controllers = $this->controllerlist->getControllers(); | |
$totalMethods = 0; | |
$totalControllers = 0; | |
echo '<pre>'; | |
foreach ($controllers as $controller => $methods) { | |
$methodCount = count($methods); | |
$totalMethods += $methodCount; | |
$totalControllers++; | |
echo "Controller: " . $controller . " (Methods: $methodCount)" . PHP_EOL; | |
echo "Routes:" . PHP_EOL; | |
foreach ($methods as $method) { | |
echo " " . $controller . '/' . $method . PHP_EOL; | |
} | |
echo PHP_EOL; // Add an extra line for readability | |
} | |
echo "Total Controllers: $totalControllers" . PHP_EOL; | |
echo "Total Methods: $totalMethods" . PHP_EOL; | |
echo '</pre>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment