简体   繁体   中英

Symfony 3.4 - The service “AppBundle\Controller\[some]Controller” has a dependency on a non-existent service “Symfony\Component\Serializer\Serializer”

(1/1) ServiceNotFoundException The service "AppBundle\\Controller\\FormController" has a dependency on a non-existent service "Symfony\\Component\\Serializer\\Serializer".

My controller is:

<?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'

For me error does not make any sense. We can clearly see that there is no such dependency in the controller.

How to fix this?

Your SubscriberRepository has a dependency on serializer. Because serializer service is not available by default, you have to turn it on, activate it in your configuration before using:

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

More Information: http://symfony.com/doc/3.4/serializer.html

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM