通过 Twig 扩展中的自定义存储库获取数据
我想在 symfony 2 Web 应用程序的每个页面上显示新通知。 有人建议我为此使用 Twig Extension。我在该扩展中创建了一个函数 getFriendRequests,但我不知道通过 twig 扩展中的自定义存储库获取数据是否是一个好习惯:现在它给了我错误,它找不到 getDoctrine 方法。
<?php
namespace Tennisconnect\DashboardBundle\Extension;
class NotificationTwigExtension extends \Twig_Extension
{
public function getFriendRequests($user)
{
$users = $this->getDoctrine()
->getRepository('TennisconnectUserBundle:User')
->getFriendRequests();
return count($users);
}
public function getName()
{
return 'notification';
}
public function getFunctions()
{
return array(
'getFriendRequests' => new \Twig_Function_Method($this, 'getFriendRequests'));
}
}
I'd like to display new notifications on every page of my symfony 2 webapplication.
I was advised to use a Twig Extension for this. I've created a function getFriendRequests in that extension, but I don't know if it's good practice to get data through my custom repository in the twig extension: Right now it's giving me the error, that it can't find the getDoctrine method.
<?php
namespace Tennisconnect\DashboardBundle\Extension;
class NotificationTwigExtension extends \Twig_Extension
{
public function getFriendRequests($user)
{
$users = $this->getDoctrine()
->getRepository('TennisconnectUserBundle:User')
->getFriendRequests();
return count($users);
}
public function getName()
{
return 'notification';
}
public function getFunctions()
{
return array(
'getFriendRequests' => new \Twig_Function_Method($this, 'getFriendRequests'));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为直接从树枝扩展中获取数据并没有那么糟糕。毕竟,如果您不在这里执行此操作,则需要之前获取这些记录,然后将它们传递给扩展程序以进行显示。
重要的一点是像您已经在做的那样在存储库中执行 DQL/SQL 操作。这对于将数据库语句与项目的其他部分分开非常重要。
您遇到的问题是该类中不存在方法
getDoctrine
。据我了解,您从扩展了 FrameworkBundle 基本控制器的控制器中获取了此代码。 FrameworkBundle 的基本控制器定义了此方法。为了解决这个问题,您必须将正确的服务注入您的扩展中。这是基于依赖注入容器的。您当然为您的 twig 扩展定义了一个服务,类似于以下定义:
现在的技巧是像这样注入您需要的依赖项:
然后,在您的扩展中,您定义一个接收学说依赖项的构造函数:
这是依赖注入。您可以看到我之前回答的有关访问控制器外部服务的另一个问题:此处
希望这会有所帮助。
问候,
马特
I don't think it is so bad to fetch your data directly from your twig extension. After all, if you don't do it here, you will need to fetch those records before and then pass them to the extension for display anyway.
The important point is to do the DQL/SQL stuff in the repository like you are already doing. This is important to separate database statements from other part of your project.
The problem you having is that the method
getDoctrine
does not exist in this class. From what I understand, you took this code from a controller which extends theFrameworkBundle
base controller. The base controller of theFrameworkBundle
defines this method.To overcome this problem, you will have to inject the correct service into your extension. This is based on the dependency injection container. You certainly defined a service for your twig extension, something like this definition:
The trick is now to inject the dependencies you need like this:
And then, in you extension, you define a constructor that receives the doctrine dependency:
This is the concept of dependency injection. You can see another question I answered sometime ago about accessing services outside controller: here
Hope this helps.
Regards,
Matt
与 mongo 相同:
在 config.yml
和 Twig\Extensions\MiFile.php 中
The same but with mongo:
in config.yml
and in your Twig\Extensions\MiFile.php