Codeigniter-从路由文件中检查控制器函数是否存在


Codeigniter - Check whether a Controller function exists or not, from the routes file

是否有任何方法可以检查路由文件中的控制器中是否存在函数方法。我已经尝试了如下所示,但当控制器使用会话库时,我无法将其添加到路由文件中。

$urlArr = array_values(array_filter(explode('/', $_SERVER['PATH_INFO'])));
$folderName = $urlArr[0];
$controllerName = $urlArr[1];
$actionName = !empty($urlArr[2]) ? $urlArr[2] : 'index';
include_once FCPATH."system/core/Controller.php";
include_once FCPATH."application/core/MY_Controller.php";
include_once FCPATH."application/controllers/$folderName/$controllerName.php";
// Here I need to check whether the function ($actionName) exists or not

注意:不要建议将文件作为字符串进行检查并检查函数定义字符串是否存在的解决方案。

感谢您的帮助。感谢:)

假设您有Test控制器和index方法:

class Test extends CI_Controller
{
    public function index()
    {
        echo 'index';
    }
}

由于PHP>=5.3,您可以使用回调来代替正常的路由规则。要检查是否定义了方法,可以使用ReflectionClass。以下是Test控制器的示例:

$route['test'] = function()
{
    require_once FCPATH."system/core/Controller.php";
    require_once APPPATH.'controllers/Test.php';
    $rc = new ReflectionClass('Test');
    var_dump($rc->hasMethod('publicFoo')); // bool(false)
    var_dump($rc->hasMethod('index')); // bool(true)
    return 'Test/index'; // return your routing
};