【发布时间】:2011-07-24 17:20:39
【问题描述】:
我正在创建一个 MVC 框架,它通过操作的方法参数将“action”参数之后的任何 url 参数传递给请求的操作。
所以如果网址是:
host/controller_name/action_name/param1/param2
发生以下情况(当然是简化的):
$requested_controller = new controller_name();
call_user_func_array(array($requested_controller, action_name), array(param1, param2);
问题在于错误报告。如果使用错误数量的参数请求 url(动作需要两个参数,但 url 只包含一个参数,我会收到一条警告消息,然后是破坏)。
由于这是一个程序错误而不是异常,我不能以任何方式尝试/捕获它,可以吗?有没有办法在尝试运行之前检查操作方法的预期参数数量?或者我应该以完全不同的方式来攻击它?
编辑(解决方案)
$action_method_relfection = new ReflectionMethod($requested_controller, $requested_action);
if (count($path_variables) < $action_method_relfection->getNumberOfRequiredParameters() || count($path_variables) > $action_method_relfection->getNumberOfParameters()) {
// if not, redirect to 404 error
self::redirect_to_404();
}
【问题讨论】:
-
最终解决方案中唯一发现的问题可能是,如果开发人员没有在 url 中包含正确数量的参数,他们不会收到任何警告。会找到办法的。
标签: php methods parameters