0

I have an service class with the class name NewsService.

The service is configured as follows:

services:
    portal.news:
        class: xxx\NewsBundle\Service\NewsService
        arguments: ["@doctrine.orm.entity_manager"]

I use Phpstorm with symfony plugin - The plugin finds the service, but Symfony itself does not.

I get the following error message:

An exception has been thrown during the rendering of a template ("You have requested a non-existent service "portal.news".").

How I use the service:

  1. {{ render(controller('xxBundle:Widget:renderNews', {'slice_length': 250})) }}
  2. in the Controller xxBundle:Widget:renderNews: $articles = $this->get('portal.news')->getNewestArticles($count);

    • cache is cleared
    • I checked everything (wrong service configuration, bundle is loaded, syntax is ok, ...)
4

2 回答 2

2

你可能忘了设置public: true你的服务,因为从 3.4 开始,所有 Symfony 服务默认都是私有的。

此外,您应该避免$this->get()使用函数,而是更喜欢直接从控制器参数中获取服务

<?php 
use xxx\NewsBundle\Service\NewsService
class MyController {
    public function myAction(NewsService $service)  {}
}
于 2018-01-21T13:33:32.660 回答
0

使用需要参数的服务,您需要将您的服务声明为别名https://symfony.com/doc/current/service_container.html#explicitly-configuring-services-and-arguments

于 2018-01-21T16:25:33.607 回答