Symfony 4:您请求了一个不存在的服务

Symfony 4 : You have requested a non-existent service

我将我的网站从 Symfony 3.4 更新到 Symfony 4 并做了一点改动。但是知道我有这个错误:

您请求了一项不存在的服务"D"。

这是我的堆栈跟踪:

Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException:
You have requested a non-existent service "D".

  at vendor\symfony\dependency-injection\Container.php:263
  at Symfony\Component\DependencyInjection\Container->get('D')
     (vendor\symfony\routing\Loader\DependencyInjection\ServiceRouterLoader.php:38)
  at Symfony\Component\Routing\Loader\DependencyInjection\ServiceRouterLoader->getServiceObject('D')
     (vendor\symfony\routing\Loader\ObjectRouteLoader.php:55)
  at Symfony\Component\Routing\Loader\ObjectRouteLoader->load('D:\IIS Websites\Wizard\src/config/routing.yml', 'service')
     (vendor\symfony\config\Loader\DelegatingLoader.php:40)
  at Symfony\Component\Config\Loader\DelegatingLoader->load('D:\IIS Websites\Wizard\src/config/routing.yml', 'service')
     (vendor\symfony\framework-bundle\Routing\DelegatingLoader.php:70)
  at Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader->load('D:\IIS Websites\Wizard\src/config/routing.yml', 'service')
     (vendor\symfony\framework-bundle\Routing\Router.php:56)
  at Symfony\Bundle\FrameworkBundle\Routing\Router->getRouteCollection()
     (vendor\symfony\routing\Router.php:367)
  at Symfony\Component\Routing\Router->getMatcherDumperInstance()
     (vendor\symfony\routing\Router.php:289)
  at Symfony\Component\Routing\Router->Symfony\Component\Routing\{closure}(object(ResourceCheckerConfigCache))
  at call_user_func(object(Closure), object(ResourceCheckerConfigCache))
     (vendor\symfony\config\ResourceCheckerConfigCacheFactory.php:43)
  at Symfony\Component\Config\ResourceCheckerConfigCacheFactory->cache('D:\IIS Websites\Wizard\var\cache\dev/srcDevDebugProjectContainerUrlMatcher.php', object(Closure))
     (vendor\symfony\routing\Router.php:302)
  at Symfony\Component\Routing\Router->getMatcher()
     (vendor\symfony\routing\Router.php:256)
  at Symfony\Component\Routing\Router->matchRequest(object(Request))
     (vendor\symfony\http-kernel\EventListener\RouterListener.php:109)
  at Symfony\Component\HttpKernel\EventListener\RouterListener->onKernelRequest(object(GetResponseEvent), 'kernel.request', object(EventDispatcher))
     (vendor\symfony\event-dispatcher\EventDispatcher.php:212)
  at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(array(object(DebugHandlersListener), 'configure'), array(object(ValidateRequestListener), 'onKernelRequest'), array(object(SessionListener), 'onKernelRequest'), array(object(FragmentListener), 'onKernelRequest'), array(object(RouterListener), 'onKernelRequest'), array(object(ResolveControllerNameSubscriber), 'onKernelRequest'), array(object(LocaleListener), 'onKernelRequest'), array(object(TranslatorListener), 'onKernelRequest'), array(object(TraceableFirewallListener), 'onKernelRequest')), 'kernel.request', object(GetResponseEvent))
     (vendor\symfony\event-dispatcher\EventDispatcher.php:44)
  at Symfony\Component\EventDispatcher\EventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
     (vendor\symfony\http-kernel\HttpKernel.php:125)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor\symfony\http-kernel\HttpKernel.php:66)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor\symfony\http-kernel\Kernel.php:190)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public\index.php:34)

提前感谢您的帮助。

提前致歉,因为我没有实际答案,但这不适合发表评论。

// From the stack trace
ObjectRouterLoader->load(D:\blahblah) 

// ObjectRouterLoader::load()
public function load($resource, $type = null)
{
    $parts = explode(':', $resource);
    if (2 != count($parts)) {
        throw new \InvalidArgumentException(sprintf('Invalid resource "%s" passed to the "service" route loader: use the format "service_name:methodName"', $resource));
    }

    $serviceString = $parts[0];
    $method = $parts[1];

    $loaderObject = $this->getServiceObject($serviceString);

路由对象加载器实际上需要一个像 service:method 这样的字符串。您的 windows D 驱动器路径也有一个冒号,因此它通过了初始检查,但随后想要加载名为 D 的服务。

那么为什么调用 ObjectRouteLoader::load 时使用了无效参数?我无法从堆栈跟踪中回答,但我猜你的应用程序 Kernel::configureRoutes 或者你的 routes.yaml 文件之一有问题。

我只能建议您尝试撤消与路线相关的所有操作。如果这没有帮助,那么也许可以从一个新的 S4 骨架项目开始,并一次将您的 S3 应用程序移动一点。

我在从 Symfony 3.4 升级到 4.0 时遇到了同样的问题。我目前正在为路由使用注释,而在使用 Symfony 3 时情况并非如此。

看来我的 framework.yaml 路由器配置 (/config/packages/) 有误。

framework: router: resource: "%kernel.root_dir%/config/routing.yml" strict_requirements: ~

它实际上是来自旧 config.yml 文件的 copy/paste。由于我正在为路由使用注释,因此在框架配置中删除路由器密钥解决了我的问题。但您可能还需要指向 'resource'.

下的特定路由配置文件

您可以使用以下命令检查指定的资源路径是否正确:

php bin/console debug:config framework

希望这会有所帮助!