2
votes

I have Difficulty to add notifications in symfony

I use this bundle https://github.com/maximilienGilet/notification-bundle

I have followed the doc, but i have encountered difficulty

THis what i have added in controller ( add annonce)

        $manager = $this->get('mgilet.notification');
        $notif = $manager->createNotification('Nouveau candidat !');
        $notif->setMessage('X a entré un candidat');
        $notif->setLink('http://symfony.com/');
        $manager->addNotification(array($this->getUser()), $notif, true);

        return $this->redirectToRoute('index');

And this what i have added in twig {{ mgilet_notification_render(app.user) }}

But after adding annonce i see this error

Service "mgilet.notification" not found: even though it exists in the app's container, the container inside "App\Controller\TestController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session", "templating" and "twig" services. Try using dependency injection instead.

1
Symfony version?gp_sflover
i use symfony 4Khqlil

1 Answers

0
votes

In the controller function try using dependency injection instead like this:

public function controllerFunction(Mgilet\NotificationBundle\Manager\NotificationManager $manager)
{
    $notif = $manager->createNotification('Nouveau candidat !');
    $notif->setMessage('X a entré un candidat');
    $notif->setLink('http://symfony.com/');
    $manager->addNotification(array($this->getUser()), $notif, true);

    return $this->redirectToRoute('index');
}