Codeigniter - ตรวจสอบว่ามีฟังก์ชัน Controller หรือไม่จากไฟล์เส้นทาง

มีวิธีใดที่จะตรวจสอบว่ามี ฟังก์ชัน หรือ วิธีการ อยู่ใน ตัวควบคุม จากไฟล์ เส้นทาง ฉันได้ลองตามที่แสดงด้านล่าง แต่ค้างเมื่อคอนโทรลเลอร์ใช้ไลบรารี เซสชัน ซึ่งฉันไม่สามารถเพิ่มลงในไฟล์เส้นทางได้

$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

หมายเหตุ: อย่าแนะนำวิธีแก้ปัญหาในการตรวจสอบไฟล์เป็นสตริงและตรวจสอบว่ามีสตริงคำจำกัดความของฟังก์ชันอยู่หรือไม่

ความช่วยเหลือใด ๆ ที่ชื่นชม ขอบคุณ :)


person Sanjay Kumar N S    schedule 07.04.2016    source แหล่งที่มา


คำตอบ (1)


สมมติว่าคุณมีตัวควบคุม 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
};
person Alexander Popov    schedule 07.04.2016