I created new bundle (AcmeNotificationBundle) and I want to use it like a service like this:
$notification = $this->get( 'notification' );
$mess = $notification->getNotification( 'Some notification message' )->createView();
But in my bundle I need a twig service to render notification templates. I understand that I need something like this in Resources\config\services.yml file:
services:
twig:
class: Path\To\Twig\Class
But I don’t know what is the right path to twig class. Аnyone encountered this problem? What is the right way to add twig service to bundle?
Templating service is already avaiable in your bundle. You can retrieve it from the container:
You should be able to access twig service in a similar way:
Rest of my answer uses templating service but you can easily replace it with twig if you really need to.
I think what you need is passing templating service to your notification service.
Your Notification class would take the templating as a constructor parameter:
Instead of
$notification->getNotification('Some notification message')->createView()I’d probably do$notification->createNotificationView('Some notification message'). I’m assuming notification message is an entity and there’s no need to pass templating to the entity.Related documentation: Referencing (Injecting) Services