user4144415
user4144415

Reputation:

Symfony 3.4: Correctly configured service is not found

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, ...)

Upvotes: 1

Views: 2838

Answers (2)

Smaïne
Smaïne

Reputation: 1399

Using Service that needs an argument, you need to declare your service as an alias https://symfony.com/doc/current/service_container.html#explicitly-configuring-services-and-arguments

Upvotes: 1

Fabien Papet
Fabien Papet

Reputation: 2319

You probably forgot to set public: true to your service, because since 3.4 all Symfony services are private by Default.

Also, you should avoid $this->get() functions and prefer fetching directly your service from your controller arguments

<?php 
use xxx\NewsBundle\Service\NewsService
class MyController {
    public function myAction(NewsService $service)  {}
}

Upvotes: 2

Related Questions