Reputation: 19251
I am working on creating an MVC framework, which passes any url parameters after the "action" parameter to the requested action via the action's method parameters.
so if the url is:
host/controller_name/action_name/param1/param2
the following takes place (simplified of course):
$requested_controller = new controller_name();
call_user_func_array(array($requested_controller, action_name), array(param1, param2);
The problem is in error reporting. If an url is requested with the wrong number of parameters (action expects two parameters, but url only contains one parameter, i get a warning message, then havoc).
Since this is a procedural error instead of an exception, I cannot try/catch it in any way, can I? Is there a way to check the number of expected parameters for the action method before trying to run it? Or should I be attacking this in an entirely different way?
EDIT (SOLUTION)
$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();
}
Upvotes: 2
Views: 733
Reputation: 91734
Although you can use the func_get_args()
function, it kind of feels the wrong way to go about it.
I would always make sure what function I´m calling and what (optional...) arguments it needs.
Upvotes: 0
Reputation: 21449
Try something simple like
if (!isset($param1) || !isset($param2)) exit;
Upvotes: 0
Reputation: 400972
I suppose that, using the Reflection API, you could find out how many parameters each method accepts, using some combination of
This way, you always pass one parameter, all methods always expect one parameter ; and no warning / error anymore.
Upvotes: 5