Reputation: 2522
In Fat Free Framework code and examples online I sometimes see URL parameters referenced like this:
route_func($f3, $args) {
echo $args['name']
}
i also see:
route_func($f3, $args) {
$param=$f3->get('PARAMS.name');
echo $param;
}
Which method is preferred? Are there any caveats to one or the other?
Upvotes: 1
Views: 2351
Reputation: 3908
The PARAMS
variable can be accessed from anywhere in the code, so $f3->get('PARAMS.name')
works everywhere.
Anyway, for convenience purpose, at routing time the route parameters are passed to the route handler. So you can spare one line of code by using the 2nd argument passed to the route handler.
In other words, the 2 examples you provided are equivalent, so choose the one that you understand best.
See this answer for more details about arguments passed at routing time.
NOTE:
As @user3587554 suggested, the 2 syntaxes differ on the treatment of non-existing keys: $args['name']
throws an error while $f3->get('PARAMS.name')
returns NULL. So to be perfectly identical, the first syntax should be @$args['name']
. But most of the time, this precaution is useless since there's no doubt about the parameter name(s).
Upvotes: 3