Reputation: 43225
This is doable by recursively reading filenames in PHP. But is there any already existing method in Router Class or some other class which can give me names of all controllers ?
Background:
I want to assign URLs to users like :
http://www.example.com/my_user_name
But would not want to have any my_user_name
equal to any of CI controllers.
Upvotes: 2
Views: 586
Reputation: 1067
It`s possible using pragmatically - please follow these steps
1) Create this library with ControllerList.php and save into application/libraries directory.
Library -
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class ControllerList {
/**
* Codeigniter reference
*/
private $CI;
/**
* Array that will hold the controller names and methods
*/
private $aControllers;
// Construct
function __construct() {
// Get Codeigniter instance
$this->CI = get_instance();
// 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, EXT);
// Loop through the subdirectory
foreach(glob(APPPATH . 'controllers/'.$dirname.'/*') as $subdircontroller) {
// Get the name of the subdir
$subdircontrollername = basename($subdircontroller, EXT);
// Load the controller file in memory if it's not load already
if(!class_exists($subdircontrollername)) {
$this->CI->load->file($subdircontroller);
}
// Add the controllername to the array with its methods
$aMethods = get_class_methods($subdircontrollername);
$aUserMethods = array();
foreach($aMethods as $method) {
if($method != '__construct' && $method != 'get_instance' && $method != $subdircontrollername) {
$aUserMethods[] = $method;
}
}
$this->setControllerMethods($subdircontrollername, $aUserMethods);
}
}
else if(pathinfo($controller, PATHINFO_EXTENSION) == "php"){
// value is no directory get controller name
$controllername = basename($controller, 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 = array();
if(is_array($aMethods)){
foreach($aMethods as $method) {
if($method != '__construct' && $method != 'get_instance' && $method != $controllername) {
$aUserMethods[] = $method;
}
}
}
$this->setControllerMethods($controllername, $aUserMethods);
}
}
}
}
?>
2) Now load this library and using that you can fetch all controllers and methods accordingly.
$this->load->library('controllerlist');
print_r($this->controllerlist->getControllers());
Output will be like that -
Array
(
[academic] => Array
(
[0] => index
[1] => addno
[2] => addgrade
[3] => viewRecordByStudent
[4] => editStudentRecord
[5] => viewRecordByClass
[6] => viewRecordByTest
[7] => viewGradeByClass
[8] => editGrade
[9] => issueMarksheet
[10] => viewIssueMarksheet
[11] => checkRecordStatus
[12] => checkRecordData
[13] => checkStudentRecordData
[14] => insertGrades
[15] => updateGrades
[16] => updateRecords
[17] => insertStudentNo
[18] => getRecordDataByStudent
[19] => getRecordDataByClass
[20] => getGradesDataByClass
[21] => deleteGrades
[22] => insertIssueMarksheet
[23] => getIssuedMarksheets
[24] => printMarksheet
)
[attendance] => Array
(
[0] => index
[1] => holidays
[2] => deleteHoliday
[3] => addHoliday
[4] => applications
[5] => deleteApplication
[6] => addApplication
[7] => insertApplication
[8] => applcationByClass
[9] => applcationByPeriod
[10] => applcationByStudent
[11] => getApplicationsByClass
[12] => getApplicationsByPeriod
[13] => getApplicationsByStudent
[14] => attendanceforstudent
[15] => attendanceforfaculty
[16] => getStudentsForAttendance
[17] => feedStudentAttendance
[18] => sendAbsentStudents
[19] => particularStudent
[20] => monthlyWiseStudents
[21] => dailyAttedance
[22] => feedFacultyAttendance
[23] => particularFaculty
[24] => monthlyWiseFaculty
[25] => editStudentAttendance
[26] => updateStudentAttendance
)
)
please apply this and let me know if you have any issues.
Upvotes: 0
Reputation: 1197
What about using the $route['404_override'] = 'users';
that way anything not found will hit your users controller. If no user is found then just show_404().
Upvotes: 0
Reputation: 15476
There is no method within CodeIgniter that can provide you with that information.
The CodeIgniter Router tries to load the controller asked for with the passed URL segments. It doesn't load all controllers, since this would have no purpose.
A suggestion would be extending the router and adding your desired functionality.
Upvotes: 1
Reputation: 100175
Try:
$files = get_dir_file_info(APPPATH.'controllers', FALSE); // Loop through file names removing .php extension foreach (array_keys($files) as $file) { $controllers[] = str_replace(EXT, '', $file); } print_r($controllers);
Upvotes: 0