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 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备