I am working on migrating an existing Symfony 2.8
project to Symfony 3.4
and would like to autowire / auto inject the value of the %kernel.environment%
parameter into a controller action.
However binding the parameter to a argument name does not work:
// app/config/services.yml
services:
_defaults:
autowire: true
autoconfigure: true
public: false
bind:
$kernelEnvironment: '%kernel.environment%'
MyBundle\:
resource: '../../src/MyBundle/*'
exclude: '../../src/MyBundle/{Entity,Repository,Tests}'
MyBundle\Controller\:
resource: '../../src/MyBundle/Controller'
tags: ['controller.service_arguments']
// src/MyBundle/SomeController.php
public function showPostAction($kernelEnvironment, $post_id) {
...
}
Uncaught PHP Exception RuntimeException: "Controller "MyBundle\Controller\SomeController::showPostAction()" requires that you provide a value for the "$kernelEnvironment" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one."
Why does this not work?
I know that I could specify MyBundle\Controller\SomeController
directly within in the services.yml
and set the value of $kernelEnvironment
in the arguments list. However this solution would mean that I would have to specify the argument for every controller that uses this parameter. This is is not what autorwire is good for. Binding the argument should work, shouldn't it?