这可以通过在PHP中递归读取文件名来实现。但是有没有已经存在的方法在路由器类或其他类可以给我所有控制器的名称?
背景:我想给用户分配如下url:http://www.example.com/my_user_name
但不希望任何my_user_name
等于任何CI控制器
CodeIgniter中没有方法可以为您提供这些信息。
CodeIgniter路由器尝试用传递的URL段加载请求的控制器。它不会加载所有的控制器,因为这样做没有任何意义。
一个建议是扩展路由器并添加你想要的功能。
尝试:
<>之前$files = get_dir_file_info(APPPATH. txt)"控制器",假);//遍历文件名,去掉.php扩展名Foreach (array_keys($files)作为$file){$controllers[] = str_replace(EXT, ", $file);}print_r(控制器);如果使用$route['404_override'] = 'users';
,任何没有找到的东西都会击中你的用户控制器。如果没有找到用户,则执行show_404()。
可以实际使用-请遵循以下步骤
1)用ControllerList.php创建这个库,并保存到application/libraries目录。
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)现在加载这个库,使用它你可以相应地获取所有的控制器和方法。
$this->load->library('controllerlist');
print_r($this->controllerlist->getControllers());
输出如下-
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
)
)
请应用这个,如果你有任何问题请告诉我。