简体   繁体   中英

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.

Thanks guys

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM