Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正在搜索正确的控制器/操作。PHP_Php - Fatal编程技术网

正在搜索正确的控制器/操作。PHP

正在搜索正确的控制器/操作。PHP,php,Php,有一段代码: if(DataEndpoint::isAjaxRequest()) { if(isset($_POST['controller']) && !empty($_POST['controller']) && isset($_POST['action']) && !empty($_POST['action'])) { $controllerName = $_POST['controller']; $a

有一段代码:

if(DataEndpoint::isAjaxRequest()) {
    if(isset($_POST['controller']) && !empty($_POST['controller']) && isset($_POST['action']) && !empty($_POST['action'])) {
        $controllerName = $_POST['controller'];
        $actionName = $_POST['action'];

        if(class_exists($controllerName.'Controller')) {
            $controller = new $controllerName.'Controller';

            if(method_exists($controller, $actionName)) {
                // if id's been passed
                // if method signature accepts the parameter
                // invoke... ?
            }
        }
    }
} // if(DataEndpoint::isAjaxRequest()) {

我可以检查给定的
action
是否存在,但不知道如何
通过id等附加参数传递/调用该操作(我们建议它是一个字符串,是可选的)。如何解决这个问题?

使用ReflactionClass和ReflactionMethod。看看这里:

$action_ref = new ReflectionMethod($controller, $action);
$action_required_params = $action_ref->getParameters();
$parameters = array(/*...*/);
$action_ref->invokeArgs($controller, $parameters);

然后调用
ReflectionMethod::invoke