我创建了新的bundle(AcmeNotificationBundle),我想像使用这样的服务一样使用它:
$notification = $this->get( 'notification' );
$mess = $notification->getNotification( 'Some notification message' )->createView();
Run Code Online (Sandbox Code Playgroud)
但在我的包中,我需要一个twig服务来呈现通知模板.我知道我在Resources\config\services.yml文件中需要这样的东西:
services:
twig:
class: Path\To\Twig\Class
Run Code Online (Sandbox Code Playgroud)
但我不知道twig class的正确途径是什么.Аnyone遇到了这个问题?将捆绑服务添加到捆绑包的正确方法是什么?
您的捆绑包中已经提供了模板服务.您可以从容器中检索它:
$container->get('templating');
Run Code Online (Sandbox Code Playgroud)
您应该能够以类似的方式访问twig服务:
$container->get('twig');
Run Code Online (Sandbox Code Playgroud)
我的其余部分使用模板服务,但如果你真的需要,你可以轻松地用树枝替换它.
我认为您需要的是将模板服务传递给您的通知服务.
services:
notification:
class: Acme\NotificationBundle\Notification
arguments: [@templating]
Run Code Online (Sandbox Code Playgroud)
您的Notification类将模板作为构造函数参数:
use Symfony\Bundle\TwigBundle\TwigEngine;
class Notification
{
/**
* @var Symfony\Bundle\TwigBundle\TwigEngine $templating
*/
private $templating = null;
/**
* @param Symfony\Bundle\TwigBundle\TwigEngine $templating
*
* @return null
*/
public function __construct(TwigEngine $templating)
{
$this->templating = $templating;
}
}
Run Code Online (Sandbox Code Playgroud)
而不是$notification->getNotification('Some notification message')->createView()
我可能会这样做$notification->createNotificationView('Some notification message')
.我假设通知消息是一个实体,并且不需要将模板传递给实体.
相关文档:引用(注入)服务
归档时间: |
|
查看次数: |
3739 次 |
最近记录: |