寻找合适的控制器/动作。 PHP

时间:2013-06-05 08:38:32

标签: php

有一段代码:

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是否存在,但不知道to pass/invoke the action with如何附加参数如id(让我们建议它是一个字符串,它是可选的)。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

使用ReflactionClass和ReflactionMethod。看看这里:

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