duanbing8817 2018-07-16 11:52
浏览 51
已采纳

在所有控制器中调用变量

the title of the application is not very clear .... I am looking to use a smarty variable that is calling in my main controller siteController.php. This variable is calling in my banner which is calling everywhere in the project except that of course being given that the variable is calling only in my controller that displays my home page, it will not work on the other page. the current code:

/**
 * @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));
}

Template:

<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>

the 2 templates are identical ...

Service:

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);
}

I am trying to retrieve the variable message_user

How can I display it everywhere? Thank you for your help

  • 写回答

1条回答 默认 最新

  • dtef9322 2018-07-18 07:15
    关注

    You can use render controller method : https://symfony.com/doc/current/templating/embedding_controllers.html

    If this solution isn't correct, you can use session variable in your twig template : https://symfony.com/doc/3.4/templating/app_variable.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染