Reputation:
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:
{{ render(controller('xxBundle:Widget:renderNews', {'slice_length': 250})) }}
in the Controller xxBundle:Widget:renderNews
: $articles = $this->get('portal.news')->getNewestArticles($count);
Upvotes: 1
Views: 2838
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
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