这可以通过递归读取PHP中的文件名来实现。 但是在路由器类或其他类中是否存在任何已经存在的方法可以为我提供所有控制器的名称?
背景:
我想为用户分配URL,例如:
http://www.example.com/my_user_name
但是不希望任何my_user_name
等于任何CI控制器。
答案 0 :(得分:1)
CodeIgniter中没有可以为您提供该信息的方法。
CodeIgniter路由器尝试使用传递的URL段加载要求的控制器。它不会加载所有控制器,因为这没有任何意义。
建议为extending the router并添加您想要的功能。
答案 1 :(得分:0)
尝试:
$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);
答案 2 :(得分:0)
如果使用$route['404_override'] = 'users';
,那么任何未找到的内容都会影响到您的用户控制器。如果没有找到用户,那么只需show_404()。
答案 3 :(得分:0)
可以实用 - 请按照以下步骤进行操作
1)使用 ControllerList.php 创建此库并保存到 application / libraries 目录。
图书馆 -
<?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
)
)
如果您有任何问题,请申请并告诉我。