这可以通过递归读取PHP中的文件名来实现。 但是在路由器类或其他一些可以给我所有控制器名称的类中是否存在已有的方法?PHP CodeIgniter:如何以编程方式检索所有控制器的名称?
背景: 我想要的网址分配给用户,如: http://www.example.com/my_user_name
但不希望有任何my_user_name
等于任何CI控制器。
这可以通过递归读取PHP中的文件名来实现。 但是在路由器类或其他一些可以给我所有控制器名称的类中是否存在已有的方法?PHP CodeIgniter:如何以编程方式检索所有控制器的名称?
背景: 我想要的网址分配给用户,如: http://www.example.com/my_user_name
但不希望有任何my_user_name
等于任何CI控制器。
有内没有笨的方法,可以为您提供这些信息。
CodeIgniter路由器尝试加载控制器要求的传递的URL段。它不会加载所有控制器,因为这没有用处。
建议将是extending the router并添加您所需的功能。
尝试:
$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);
OP简单地说:“这是可以通过递归读取文件名在PHP中。但是有没有现有的方法..?'这是他试图避免 –
那么如何使用$route['404_override'] = 'users';
那样找不到的东西会击中你的用户控制器。如果找不到用户,那么只需show_404()。
谢谢你的建议,将尝试这个并恢复。 – DhruvPathak
但可以说www.mysite.com/USER_NAME有404,并且404覆盖进入用户控制器,我如何使用Router类在'user'控制器中检索USER_NAME? – DhruvPathak
你可以使用uri段:$ this-> uri-> segment(1)然后如果找不到,手动抛出404。 – Eric
它`可以使用务实 - 请按照以下步骤
1)ControllerList.php创建这个图书馆保存到应用程序/库目录。
图书馆 -
<?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
)
)
请申请这个,让我知道,如果你有任何问题。
有没有什么原因让你不想将它作为网址的一部分传递? – Daz
@Daz是的,'http:// www.site.com/THEUSERNAME'似乎是大多数门户网站指定虚拟网址的标准做法。考虑到这种做法,“http:// www.site.com/ONE_CONTROLLER/THEUSERNAME”似乎并不合适。 – DhruvPathak
@Daz SO在这里例外:D \ – DhruvPathak