10

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?

2 Answers 2

15

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
    }
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Thx. And what about passing parameters from controller to twig template - which way is faster pass parameters from controller, or get parameters in twig template by {{ app.request.get('name') }}?
As for the performance, same rules apply (app.request.get translates directly to something like $container->get('request')->get('name')). But for the coding style, you shouldn't rely on app variable since it's not available in all situations (try experimenting with ESI, template includes or so and you'll end in the same position as me months ago) - the best way is to send all data you need computed before the render template request.
-1

I think you should use getRequest() method because it's more dynamic !

1 Comment

getRequest() is just a shortcut for $this->container->get('request') but getting Request object from action parameters is DI approach that should probably be preferred. In fact there is no getRequest() method if you use controller as a service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.