在所有控制器中调用变量

时间:2018-07-16 11:52:23

标签: php symfony

应用程序的标题不是很清楚....我正在寻找使用在我的主控制器siteController.php中调用的smarty变量。该变量正在我的横幅中调用,该横幅正在项目中的所有地方调用,但前提是该变量仅在显示我的主页的控制器中调用,因此在其他页面上将不起作用。当前代码:

/**
 * @Route("/", name="homepage")
 */
public function indexAction(Request $request)
{
    if ($this->get('app.bdd')->parameterGetOrAdd('VERSION', '1') < 1.5 && $this->getUser()) 
      return $this->redirectToRoute('account_home');

    $em         = $this->getDoctrine()->getManager();
    $rep_user   = $em->getRepository('PublikaBDDBundle:User');
    $rep_message = $em->getRepository('PublikaBDDBundle:Message');
    $rep_region = $em->getRepository('PublikaBDDBundle:Region');
    $rep_notice = $em->getRepository('PublikaBDDBundle:Notice');
    $rep_presse = $em->getRepository('PublikaBDDBundle:Presse');
    $rep_publicitaire = $em->getRepository('PublikaBDDBundle:Publicitaire');
    $slide = $em->getRepository('PublikaBDDBundle:Slide');

    $session = $request->getSession();
    $aviva_displayed = $session->get('aviva_displayed', 0);
    if ($aviva_displayed == 0)
    {
        $session->set('aviva_displayed', 1);
    }



    $day_captain = $rep_user->getCaptainOfTheDay();
    // $day_passengers = $rep_user->getPassengersOfCaptainOfTheDay($day_captain);
    $notices     = $rep_notice->getLastNoticesForCaptain($day_captain, 3);

    $regions     = $rep_region->findBy(array(), array('name'=>'ASC'));

    $presses     = $rep_presse->getForSite();

    $publicitaires_g = $rep_publicitaire->findBy(array('hook' => 'block_gauche'), array('id' => 'DESC'),1);
    $publicitaires_c = $rep_publicitaire->findBy(array('hook' => 'block_centre_bas'), array('id' => 'DESC'),1);

    $slide_show = $slide->findBy(array(), array('id' => 'DESC'), 5);

    foreach ($regions as $region)
    {
        $min_lat = (isset($min_lat)) ? min($region->getMinLatitude(), $min_lat)  : $region->getMinLatitude();
        $max_lat = (isset($max_lat)) ? max($region->getMaxLatitude(), $max_lat)  : $region->getMaxLatitude();
        $min_lng = (isset($min_lng)) ? min($region->getMinLongitude(), $min_lng) : $region->getMinLongitude();
        $max_lng = (isset($max_lng)) ? max($region->getMaxLongitude(), $max_lng) : $region->getMaxLongitude();
    }
    $map_area_km = max($this->get('app.bdd')->calculDistanceKm($min_lat, 0, $max_lat, 0)
                     , $this->get('app.bdd')->calculDistanceKm(0, $min_lng, 0, $max_lng));

    $main_map_elems = array(
        'area_km' => $map_area_km
        , 'center_lat' => ($min_lat + $max_lat) / 2
        , 'center_lng' => ($min_lng + $max_lng) / 2
    );

    // 12/04/2017 => plus d'Aviva
    $aviva_displayed = 1;

    // On récupére les messages de l'utilisateur connecté
    $users = $rep_user->getUserMessageWith($this->getUser());
    for ($ix=0, $messages=array(); $ix<sizeof($users); $ix++)
        $messages[] = $rep_message->getLastMessageOfDiscutionBeetween($users[$ix], $this->getUser());

    return $this->render('AppSiteBundle:Home:index.html.twig'
              , array('day_captain'=>$day_captain, 'main_map_elems'=>$main_map_elems
                    , 'notices'=>$notices, 'presses'=>$presses,
                    'messages_user' => $messages,
                    'publicitaires_g' => $publicitaires_g,
                    'publicitaires_c' => $publicitaires_c,
                    'slides' => $slide_show
                    , 'aviva_displayed'=>$aviva_displayed));
}

模板:

<ul class="list-inline pull-right">

                    <li><a class="mon-compte" href="{% if app.user is null %}{{ path('login_route') }}{% else %}{{ path('account_home') }}{% endif %}">{{ 'site.top_link.myaccount'|trans }}</a></li>

                    {% if app.user is null %}
                        {% if current_version >= 2 %}
                        <li><a class="devenir-passager" href="{{ path('subscribe_as_passenger') }}">{{ 'site.top_link.subscribe_passenger'|trans }}</a></li>
                        {% endif %}
                        <li><a class="devenir-capitaine" href="{{ path('subscribe_as_captain') }}">{{ 'site.top_link.subscribe_captain'|trans }}</a></li>
                    {% else %}
                        <li class="messages_show">
                            <a class="messages" href="{{ path('account_messages_list') }}">Mes messages
                            {% if service_bdd.messageCountNotViewed(app.user) > 0 %}
                                <span class="label red-clair rounded ">{{ service_bdd.messageCountNotViewed(app.user) }}</span>
                            {% endif %}
                            </a>
                            {{ app.user.getMessages }}
                            {#<p class="messages_hover">
                                {% for message in messages_user %}
                                    {% if loop.index % 2 == 0 %}{% set class = '' %}{% else %}{% set class= 'message-impair' %}{% endif %}
                                    {{ include("AppSiteBundle:Message:message_hover.html.twig", {"message": message, 'class': class}) }}
                                {% endfor %}
                                {% if service_bdd.messageCountNotViewed(app.user) <= 0 %}
                                    <a>{{ 'Pas de nouveau message'|trans }}</a>
                                {% endif %}
                            </p>#}
                        </li>
                    {% endif %}
                </ul>

这两个模板是相同的...

服务:

public function getMessages()
{
    $em         = $this->getDoctrine()->getManager();
    $rep_message = $em->getRepository('PublikaBDDBundle:Message');
    $rep_user   = $em->getRepository('PublikaBDDBundle:User');

    // On récupére les messages de l'utilisateur connecté
    $users = $rep_user->getUserMessageWith($this->getUser());
    for ($ix=0, $messages=array(); $ix<sizeof($users); $ix++)
        $messages[] = $rep_message->getLastMessageOfDiscutionBeetween($users[$ix], $this->getUser());

    return array('messages_user' => $messages);
}

我正在尝试检索变量message_user

如何在各处显示它?谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可以使用渲染控制器方法:https://symfony.com/doc/current/templating/embedding_controllers.html

如果此解决方案不正确,则可以在树枝模板中使用会话变量:https://symfony.com/doc/3.4/templating/app_variable.html