(1/1)ServiceNotFoundException服务 “AppBundle \ Controller \ FormController”依赖于a 不存在的服务“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'
对我来说错误没有任何意义。我们可以清楚地看到控制器中没有这种依赖性。
如何解决这个问题?
答案 0 :(得分:2)
您的SubscriberRepository
依赖于序列化程序。由于序列化程序服务默认情况下不可用,因此必须先将其打开,然后在使用前激活它:
# app/config/config.yml
framework:
# ...
serializer:
enabled: true
更多信息:http://symfony.com/doc/3.4/serializer.html
谢谢!