symfony2 getting route parameters (controller arguments vs request)

controller, parameters, php, routes, symfony

Solution

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
    }
  }

Problem

Which access to route parameters is faster? - Put route parameters as controller arguments - 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?

Original source