Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony2 getting route parameters (controller arguments vs request)

Which access to route parameters is faster?

  1. Put route parameters as controller arguments
  2. Getting route parameters from $this->getRequest()->get('param')

And what about request object? Better way is put request object as controller parameter or call getRequest() method on controller object?

like image 618
NHG Avatar asked Mar 15 '13 20:03

NHG


1 Answers

One could argue that since you need to pull Request object from container it's slower approach, but I've done both and difference is negligible. When you need Request object it's better to put it as the controller method argument, because you'll have it immediately and PHP Type Hinting will provide additional info (autocomplete and so on) in decent IDEs (I personally recommend PHPStorm). This applies also to other controller method arguments, you are given straight variables, no need to pull them twice from other places.

class SthController extends Controller
  {
  public function indexAction(Request $request, $arg1, $arg2)
    {
    // you have $request object with type hint and all goodness
    }
  }
like image 149
Tomasz Kowalczyk Avatar answered Sep 27 '22 17:09

Tomasz Kowalczyk