Allow binding scalar values to controllers¶

When using service autowiring, the _defaults.bind option allows to bind arguments by name or type. For example, you can define the value of $projectDir once in your app and every service that uses a constructor argument with that name will use its value:

1 2 3 4 5 # config/services.yaml services : _defaults : bind : $projectDir : '%kernel.project_dir%'

However, if some controller action defines a $projectDir argument, this configuration doesn't apply to it and the argument is not autowired:

1 2 3 4 5 6 7 /** * @Route("/do-something") */ public function somethingAction ( $projectDir ) { // the $projectDir argument is not given the configured value }

In controllers, you needed to use the $this->getParameter('kernel.project_dir') shortcut or pass the value through the controller's __construct() method. This behavior didn't provide a consistent experience (service constructor binding behaved differently than controller action binding) and it was the last rough edge about autowiring.