1

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?

ethanpil
  • 2,522
  • 2
  • 24
  • 34
  • overhead of instantiating the object and the method call. slight speed reduction. Other wise it's oop and more easy to use code vs guessing if the key exists in the array. – user3587554 May 12 '14 at 11:12

1 Answers1

3

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).

Community
  • 1
  • 1
xfra35
  • 3,833
  • 20
  • 23