Symfony 3.4.3服务容器报错:无法在控制器方法注入LoggerInterface求助
Hey there, let’s work through this service container issue you’re stuck on—injecting LoggerInterface into your controller method in Symfony 3.4.3. I’ve dealt with similar autowiring quirks in 3.4 before, so let’s break down what’s likely going wrong and how to fix it.
First, let’s look at your existing service.yml configuration. You’ve got autowiring enabled in _defaults, but Symfony 3.4 (being an LTS transition version) has specific requirements for controller autowiring that might be missing from your setup.
Step 1: Complete Your Service Registration for Controllers
Your current config starts registering src/AppBundle classes, but it’s incomplete. You need to explicitly register your controllers as services, mark them as public (since Symfony 3.4’s controller resolver requires public services), and add the controller.service_arguments tag to enable method injection. Update your service.yml like this:
# Learn more about services, parameters and containers at parameters: #parameter_name: value services: # default configuration for services in *this* file _defaults: # automatically injects dependencies in your services autowire: true # automatically registers your services as commands, event subscribers, etc. autoconfigure: true # this means you cannot fetch services directly from the container via $container->get() # if you need to do this, you can override this setting on individual services public: false # Makes all classes in src/AppBundle available as services AppBundle\: resource: '../../src/AppBundle/*' exclude: '../../src/AppBundle/{Entity,Repository,Tests}' # Explicitly configure controller services for method injection AppBundle\Controller\: resource: '../../src/AppBundle/Controller' public: true tags: ['controller.service_arguments']
Step 2: Inject LoggerInterface in Your Controller Method
Now that your controllers are properly registered, you can directly inject LoggerInterface into any action method. Here’s an example:
// src/AppBundle/Controller/YourController.php namespace AppBundle\Controller; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Response; class YourController extends Controller { public function testAction(LoggerInterface $logger) { // Use the logger $logger->info('Successfully injected LoggerInterface!'); return new Response('Check your logs!'); } }
Step 3: Verify Key Prerequisites
Before testing, double-check these common pitfalls:
- Monolog Bundle is Installed:
LoggerInterfaceis provided by Monolog, so make sure you’ve installed it withcomposer require symfony/monolog-bundle(for Symfony 3.4, use version^3.1). - Clear Cache: Symfony 3.4’s cache can hold onto old configs. Run
php bin/console cache:clear --env=devto refresh it. - Validate Autowiring: Run
php bin/console debug:autowiring LoggerInterface—you should seemonolog.loggerlisted as the matching service.
Why This Fixes Your Error
The "needs to provide a corresponding value" error happens because Symfony wasn’t recognizing your controller as a service. Without proper service registration, it tried to resolve method parameters manually (which can’t handle interface dependencies like LoggerInterface). By registering your controllers with the right tags and public setting, you tell Symfony to use the service container to autowire dependencies correctly.
内容的提问来源于stack exchange,提问作者BruceOverflow

