【问题标题】:Symfony 3.4 - The service "AppBundle\Controller\[some]Controller" has a dependency on a non-existent service "Symfony\Component\Serializer\Serializer"Symfony 3.4 - 服务“AppBundle\Controller\[some]Controller”依赖于不存在的服务“Symfony\Component\Serializer\Serializer”
【发布时间】:2018-01-05 15:38:55
【问题描述】:

(1/1) ServiceNotFoundException 服务 “AppBundle\Controller\FormController”依赖于 不存在的服务“Symfony\Component\Serializer\Serializer”。

我的控制器是:

<?php

namespace AppBundle\Controller;

use AppBundle\Service\SubscriberService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class FormController extends Controller
{
    private $subscriber;

    public function __construct(SubscriberService $subscriber)
    {
        $this->subscriber = $subscriber;
    }

    /**
     * @Route("/", name="form")
     */
    public function indexAction(): Response
    {
        $categories = $this->subscriber->getCategories();

        return $this->render('subscription/form.html.twig', ['categories' => $categories]);
    }

    /**
     * @param Request $request
     * @return Response
     * @Route("/save", name="save_subscriber")
     */
    public function save(Request $request): Response
    {
        $this->subscriber->save($request->get('name'), $request->get('email'), $request->get('categories'));
    }
}

Services.yml:

# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
    #parameter_name: value

services:
    # default configuration for services in *this* file
    _defaults:
        # automatically injects dependencies in your services
        autowire: false
        # 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 classes in src/AppBundle available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    AppBundle\:
        resource: '../../src/AppBundle/*'
        # you can exclude directories or files
        # but if a service is unused, it's removed anyway
        exclude: '../../src/AppBundle/{Entity,Tests}'

    # controllers are imported separately to make sure they're public
    # and have a tag that allows actions to type-hint services
    AppBundle\Controller\:
        resource: '../../src/AppBundle/Controller'
        public: true
        tags: ['controller.service_arguments']

    # add more services, or override services that need manual wiring
    # AppBundle\Service\ExampleService:
    #     arguments:
    #         $someArgument: 'some_value'

    AppBundle\Service\SubscriberService:
        arguments:
            $validator: '@Symfony\Component\Validator\Validator\ValidatorInterface'
            $subscriberRepository: '@AppBundle\Repository\SubscriberRepository'

    AppBundle\Controller\FormController:
        arguments:
            $subscriber: '@AppBundle\Service\SubscriberService'

    AppBundle\Repository\SubscriberRepository:
        arguments:
            $serializer: '@Symfony\Component\Serializer\Serializer'

对我来说,错误没有任何意义。我们可以清楚的看到控制器中没有这样的依赖。

如何解决这个问题?

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    您的SubscriberRepository 依赖于序列化程序。因为默认情况下serializer服务不可用,所以你必须打开它,在使用前在你的配置中激活它:

    # app/config/config.yml
    framework:
        # ...
        serializer:
            enabled: true
    

    更多信息:http://symfony.com/doc/3.4/serializer.html

    谢谢!

    【讨论】:

    • 哇,symfony 真的应该处理他们的错误信息。非常感谢您的快速回复。我还需要在 SubscriberRepository $serializer: '@serializer' 中以这种方式设置参数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多