You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony 3.4.3服务容器报错:无法在控制器方法注入LoggerInterface求助

Fixing Symfony 3.4.3 Service Container Error When Injecting LoggerInterface into Controller Methods

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: LoggerInterface is provided by Monolog, so make sure you’ve installed it with composer 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=dev to refresh it.
  • Validate Autowiring: Run php bin/console debug:autowiring LoggerInterface—you should see monolog.logger listed 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:21:23