避免在父模板的控制器中重复相同的查询 [英] Avoid repeating the same query in controllers for parent template
问题描述
我正在尝试制作消息传递系统,但遇到了一个小问题.我有一个更大的模板来显示我的菜单和我的内容.菜单包含新消息数量,内容可以是任意页面(写新消息、收件箱、发送).
I am trying to make a messaging system and I'm facing a small problem. I have a bigger template that displays my menu and my content. The menu includes the number of new messages, and the content can be any page(compose a new message, inbox, sent).
问题是我必须通过将新收到的消息的数量传递给每个小模板来呈现每个小模板,每次调用原则并重复代码.有没有办法只将号码发送到父模板?
The problem is that I have to render each of the small templates by passing the number of new received messages to each of them, calling the doctrine each time and repeating code. Is there any way to send the number only to the parent template?
这是我的模板:
这是包含给我带来问题的 newmsg 变量的父级.
This is the parent containing the newmsg variable that gives me problems.
{% extends "::base.html.twig" %}
{% block body %}
<a href="{{ path('private_message_inbox') }}"> inbox</a> : {{ newmsg }}
<a href="{{ path('private_message_sent') }}">sent</a>
<a href="{{ path('private_message_new') }}">compose</a>
{% endblock body %}
以下是子模板的示例:
{% block body %}
{{ parent() }}
{% if messageList %}
{% for message in messageList %}
<li><a href="{{ path('private_message_view',{'msg': message.id}) }}">title</a> = {{ message.title|e }}</li>
<li>cont= {{ message.content|e }}</li>
<li>data= {{ message.date|date('d-m-Y H:m:s') }}</li>
<li>sender= {{ message.sender|e }}</li>
<hr>
{% endfor %}
{% else %}
<div>no messages</div>
{% endif %}
{% endblock body %}
问题是每个子模板都要求我提供 newmsg 变量
The problem is that each child template is asking me for the newmsg variable
$messages = $this->getDoctrine()->getRepository('MedAppCrudBundle:Message');
$newMessagesNo = count($messages->findBy(array('seen' => '0', 'receiver' => $this->getUser())));
return $this->render(
'MedAppCrudBundle:UserBackend\Message:new.html.twig',
array(
'form' => $form->createView(),
'newmsg' => $newMessagesNo,
)
);
而且我必须在每个控制器中都写这个.有什么办法可以缩短这个问题吗?
And I have to write this in every single controller. Any way I can shorten this problem?
推荐答案
您可以实现一个服务,该服务返回 newmsg
值并在您的父模板上调用它.那么就没有必要传递变量了.
You could implement a service that gives back the newmsg
value and call it on your parent template. Then it would not be necessary to pass the variable.
您可以在包的 services.yml
中添加一个服务,例如:
You can add a service in your bundle's services.yml
with something like:
services:
newmessages:
class: Full\Class\Name\NewMessagesService
arguments: ["@doctrine.orm.entity_manager"]
然后,实现Full\Class\Name\NewMessagesService
类.请记住,此类将需要一个接收 EntityManager
参数的构造函数.类似的东西:
Then, implement the Full\Class\Name\NewMessagesService
class. Keep in mind that this class will need a constructor that receives an EntityManager
argument. Something like:
<?php
namespace Full\Class\Name;
class NewMessagesService{
private $entityManager;
public function __construct($entityManager){
$this->entityManager = $entityManager;
}
public function methodToCalculate(){
//Perform calculation and return result
}
}
然后,在您的父模板中,将 {{newmsg}
替换为:
Then, in your parent template, replace {{newmsg}
with:
{{ newmessages.methodToCalculate() }}
这篇关于避免在父模板的控制器中重复相同的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!