phpsymfonyservicesymfony4

How to call a service in Symfony 4?


I want to generate XML feeds in Symfony 4 and I think that the FeedBundle is more than enough for me. I installed and configured it without any problem, my problem comes when I want to call the service from the Controller, his documentation tells to do the following:

$feed = $this->get('eko_feed.feed.manager')->get('article');

But in Symfony 4 that's not the right way to do as far as I know. Could someone let me know the right way to do it? I only need to know how to call the service from the controller, nothing more.


Solution

  • You can try inject service in controller.

    Try adding in services.yaml:

    services:
    
            Eko\FeedBundle\Feed\FeedManager: '@eko_feed.feed.manager'
    

    In controller:

    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\HttpFoundation\Request;
    
    
        public function index(Request $request, \Eko\FeedBundle\Feed\FeedManager $feedmanager)
        {
            $articles = $this->getDoctrine()->getRepository('App:YourEntity')->findAll();
    
            $feed = $feedmanager->get('article');
            $feed->addFromArray($articles);
    
            return new Response($feed->render('rss'));
        }
    

    See here