I am working on upgrade Symfony 3.3 to 4.4 and almost done with it, but I have one last issue. I enabled autowiring with default config, but whole project uses lots of @ParamConverter conversions without annotation.
Problem: ParamConverter with auto_convert tries to convert services and classes that are mentioned to controller by typehinting for autowiring (not entities).
Controller:
/**
* @Route("/report", name="report_page")
*/
public function report(
Request $request,
FileManager $fileManager
): Response {
// code
}
Error:
The class 'App\Service\FileManager' was not found in the chain configured namespaces App\Entity.
Service 'App\Service\FileManager' exists and works correctly.
ParamConverter config (by default 'request: {converters: true, auto_convert: true}'):
sensio_framework_extra:
router:
annotations: false
Doctrine config:
doctrine:
dbal:
default_connection: default
connections:
default:
# config
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
Base:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: App\Entity
Controller setting (services.yaml):
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/*'
exclude: '../src/{DataFixtures,Entity,Objects,Repository,Utils}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
I understand that I can set 'sensio_framework_extra.request.auto_convert' to false and use @ParamConverter annotation everywhere in controllers, but there are too many places with this conversion and I try to get default behavior, so additional @ParamConverter annotations would be extra. Just in case: I tried to disable auto_convert and everything worked correctly (services were autowired and entities were passed to controllers).
Also I checked that I installed last available versions of packages and bundles.
Need some hints on it. Probably I missed something. I tried to find exact way how ParamConverter works by default and how it checks that class is entity or not, but it is too complicated and I decided that I dug too deep.
EDIT:
Probably I have some issue with autowiring's config and not paramconverter. But I checked php bin/console debug:autowiring
and got all needed services as available via autowiring.
I was facing the same issue and was wondering why the autowiring did not simply work as expected, even when the respective service is correctly defined and available.
It turns out that there was an incompatibility due to our used FOSRestBundle (see here). We updated FOSRestBundle and SensioFrameWorkExtraBundle to 2.4 and 4.0 respectively. However, this required that we had to add missing ParamConverter annotations.
The problem could also be avoided by disabling the auto conversion, as follows (but it might be risky):
sensio_framework_extra:
request:
# disable auto-conversion of params to entities since it would try to convert services as well...
auto_convert: false
Disabling the autoconversion is also documented here