douzi2778 2017-03-14 14:21
浏览 33

变量“沙龙”不存在 - (树枝错误) - 获取salon_id

In my layout I would like to get the view of the salon_edit by the id salon. Here is the symfony2 error "Variable "salon" does not exist" at the lign 61 -> <a href="{{ path('salon_edit', { 'id':salon.id }) }}" class="link_pink">Mon compte salon</a>

Could you help me please ?

Here is the twig's portion with this lign

<ul class="nav navbar-nav navbar-right">
                        <li>
                            {% if is_granted('ROLE_SALON') %}
                                <a href="{{ path('salon_edit', { 'id':salon.id }) }}" class="link_pink">Mon compte salon</a>
                            {% elseif is_granted('ROLE_USER') %}
                                <a href=# class="link_pink">Mon compte</a>
                            {% endif %}
                        </li>
                    {% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
                        <li>
                            <a href="{{ path('fos_user_security_logout') }}" class="link_rouge">Se déconnecter</a>
                        </li>
                    {% else %}
                        <li>
                            <a class="link_pink" href="{{ path('user_inscription') }}">S'inscrire</a>
                        </li>
                    {% endif %}
                </ul>

Here is my function from saloncontroller

    /**
 * Displays a form to edit an existing salon entity.
 *
 * @Route("/{id}/edit", name="salon_edit")
 * @Method({"GET", "POST"})
 */
public function editAction(Request $request, Salon $salon)
{
    $deleteForm = $this->createDeleteForm($salon);
    $editForm = $this->createForm('SalonBundle\Form\SalonType', $salon);
    $editForm->handleRequest($request);

    if ($editForm->isSubmitted() && $editForm->isValid()) {
        $this->getDoctrine()->getManager()->flush();

        return $this->redirectToRoute('salon_edit', array('id' => $salon->getId()));
    }

    return $this->render('@Salon/salon/edit.html.twig', array(
        'salon' => $salon,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

The stack trace

[1] Twig_Error_Runtime: Variable "salon" does not exist.
at n/a
    in /var/www/html/salon-beaute/src/CoreBundle/Resources/views/Default/layout.html.twig line 61

at __TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82->{closure}()
    in /var/www/html/salon-beaute/app/cache/dev/twig/66/66beb7ecc8aa1a0e9315e0eddb676910b6afc28c60b27e59e4cc417df7bde814.php line 128

at __TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82->block_header(array('app' => object(AppVariable)), array('title' => array(object(__TwigTemplate_5cc24ee05b63b1a2f964871f6daa7de9297002ce6686929eddf463a4e536a528), 'block_title'), 'stylesheets' => array(object(__TwigTemplate_5cc24ee05b63b1a2f964871f6daa7de9297002ce6686929eddf463a4e536a528), 'block_stylesheets'), 'header' => array(object(__TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82), 'block_header'), 'body' => array(object(__TwigTemplate_1dd3d918e01d77f295bef77124491b666956198f387af3b7a6bd384c0793c7a6), 'block_body'), 'javascripts' => array(object(__TwigTemplate_5cc24ee05b63b1a2f964871f6daa7de9297002ce6686929eddf463a4e536a528), 'block_javascripts'), 'footer' => array(object(__TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82), 'block_footer')))
    in /var/www/html/salon-beaute/app/cache/dev/classes.php line 4577

at Twig_Template->displayBlock('header', array('app' => object(AppVariable)), array('title' => array(object(__TwigTemplate_5cc24ee05b63b1a2f964871f6daa7de9297002ce6686929eddf463a4e536a528), 'block_title'), 'stylesheets' => array(object(__TwigTemplate_5cc24ee05b63b1a2f964871f6daa7de9297002ce6686929eddf463a4e536a528), 'block_stylesheets'), 'header' => array(object(__TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82), 'block_header'), 'body' => array(object(__TwigTemplate_1dd3d918e01d77f295bef77124491b666956198f387af3b7a6bd384c0793c7a6), 'block_body'), 'javascripts' => array(object(__TwigTemplate_5cc24ee05b63b1a2f964871f6daa7de9297002ce6686929eddf463a4e536a528), 'block_javascripts'), 'footer' => array(object(__TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82), 'block_footer')))
    in /var/www/html/salon-beaute/app/cache/dev/twig/7a/7a92fc34297bb697cff5b7a080558e7f9d95ea471251008ab1f7e7390b0deb5a.php line 51

at __TwigTemplate_5cc24ee05b63b1a2f964871f6daa7de9297002ce6686929eddf463a4e536a528->doDisplay(array('app' => object(AppVariable)), array('title' => array(object(__TwigTemplate_5cc24ee05b63b1a2f964871f6daa7de9297002ce6686929eddf463a4e536a528), 'block_title'), 'stylesheets' => array(object(__TwigTemplate_5cc24ee05b63b1a2f964871f6daa7de9297002ce6686929eddf463a4e536a528), 'block_stylesheets'), 'header' => array(object(__TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82), 'block_header'), 'body' => array(object(__TwigTemplate_1dd3d918e01d77f295bef77124491b666956198f387af3b7a6bd384c0793c7a6), 'block_body'), 'javascripts' => array(object(__TwigTemplate_5cc24ee05b63b1a2f964871f6daa7de9297002ce6686929eddf463a4e536a528), 'block_javascripts'), 'footer' => array(object(__TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82), 'block_footer')))
    in /var/www/html/salon-beaute/app/cache/dev/classes.php line 4682

at Twig_Template->displayWithErrorHandling(array('app' => object(AppVariable)), array('title' => array(object(__TwigTemplate_5cc24ee05b63b1a2f964871f6daa7de9297002ce6686929eddf463a4e536a528), 'block_title'), 'stylesheets' => array(object(__TwigTemplate_5cc24ee05b63b1a2f964871f6daa7de9297002ce6686929eddf463a4e536a528), 'block_stylesheets'), 'header' => array(object(__TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82), 'block_header'), 'body' => array(object(__TwigTemplate_1dd3d918e01d77f295bef77124491b666956198f387af3b7a6bd384c0793c7a6), 'block_body'), 'javascripts' => array(object(__TwigTemplate_5cc24ee05b63b1a2f964871f6daa7de9297002ce6686929eddf463a4e536a528), 'block_javascripts'), 'footer' => array(object(__TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82), 'block_footer')))
    in /var/www/html/salon-beaute/app/cache/dev/classes.php line 4663

at Twig_Template->display(array('app' => object(AppVariable)), array('header' => array(object(__TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82), 'block_header'), 'footer' => array(object(__TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82), 'block_footer'), 'body' => array(object(__TwigTemplate_1dd3d918e01d77f295bef77124491b666956198f387af3b7a6bd384c0793c7a6), 'block_body')))
    in /var/www/html/salon-beaute/app/cache/dev/twig/66/66beb7ecc8aa1a0e9315e0eddb676910b6afc28c60b27e59e4cc417df7bde814.php line 28

at __TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82->doDisplay(array('app' => object(AppVariable)), array('header' => array(object(__TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82), 'block_header'), 'footer' => array(object(__TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82), 'block_footer'), 'body' => array(object(__TwigTemplate_1dd3d918e01d77f295bef77124491b666956198f387af3b7a6bd384c0793c7a6), 'block_body')))
    in /var/www/html/salon-beaute/app/cache/dev/classes.php line 4682

at Twig_Template->displayWithErrorHandling(array('app' => object(AppVariable)), array('header' => array(object(__TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82), 'block_header'), 'footer' => array(object(__TwigTemplate_ff527ec479eaa508e6f0a4dde49c75ec7eaccfdd5401a5e2f3c7a5571c049b82), 'block_footer'), 'body' => array(object(__TwigTemplate_1dd3d918e01d77f295bef77124491b666956198f387af3b7a6bd384c0793c7a6), 'block_body')))
    in /var/www/html/salon-beaute/app/cache/dev/classes.php line 4663

at Twig_Template->display(array('app' => object(AppVariable)), array('body' => array(object(__TwigTemplate_1dd3d918e01d77f295bef77124491b666956198f387af3b7a6bd384c0793c7a6), 'block_body')))
    in /var/www/html/salon-beaute/app/cache/dev/twig/ee/ee83aa46a744063fbc1dae1a5d8c7745a1257cd4f8dc9948b21b4f2f065ab756.php line 27

at __TwigTemplate_1dd3d918e01d77f295bef77124491b666956198f387af3b7a6bd384c0793c7a6->doDisplay(array('app' => object(AppVariable)), array('body' => array(object(__TwigTemplate_1dd3d918e01d77f295bef77124491b666956198f387af3b7a6bd384c0793c7a6), 'block_body')))
    in /var/www/html/salon-beaute/app/cache/dev/classes.php line 4682

at Twig_Template->displayWithErrorHandling(array('app' => object(AppVariable)), array('body' => array(object(__TwigTemplate_1dd3d918e01d77f295bef77124491b666956198f387af3b7a6bd384c0793c7a6), 'block_body')))
    in /var/www/html/salon-beaute/app/cache/dev/classes.php line 4663

at Twig_Template->display(array())
    in /var/www/html/salon-beaute/app/cache/dev/classes.php line 4670

at Twig_Template->render(array())
    in /var/www/html/salon-beaute/vendor/symfony/symfony/src/Symfony/Bridge/Twig/TwigEngine.php line 50

at Symfony\Bridge\Twig\TwigEngine->render('@Core/Default/homepage.html.twig', array())
    in /var/www/html/salon-beaute/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/TwigEngine.php line 72

at Symfony\Bundle\TwigBundle\TwigEngine->render('@Core/Default/homepage.html.twig', array())
    in /var/www/html/salon-beaute/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/TwigEngine.php line 99

at Symfony\Bundle\TwigBundle\TwigEngine->renderResponse('@Core/Default/homepage.html.twig', array(), object(Response))
    in /var/www/html/salon-beaute/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 185

at Symfony\Bundle\FrameworkBundle\Controller\Controller->render('@Core/Default/homepage.html.twig')
    in /var/www/html/salon-beaute/src/CoreBundle/Controller/DefaultController.php line 11

at CoreBundle\Controller\DefaultController->indexAction()
    in  line 

at call_user_func_array(array(object(DefaultController), 'indexAction'), array())
    in /var/www/html/salon-beaute/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 144

at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
    in /var/www/html/salon-beaute/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 64

at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
    in /var/www/html/salon-beaute/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/DependencyInjection/ContainerAwareHttpKernel.php line 69

at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
    in /var/www/html/salon-beaute/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php line 185

at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
    in /var/www/html/salon-beaute/web/app_dev.php line 28
  • 写回答

2条回答 默认 最新

  • doude2635 2017-03-14 16:59
    关注

    You're passing an array of salons called salons. If you wanna have access to have you have to go through a twig for a loop. So in your:

    {% if is_granted('ROLE_SALON') %}
        <a href="{{ path('salon_edit', { 'id':salon.id }) }}" class="link_pink">Mon compte salon</a>
    {% elseif is_granted('ROLE_USER') %}
        <a href=# class="link_pink">Mon compte</a>
    {% endif %}
    

    you have to modify to:

    {% if is_granted('ROLE_SALON') %}
        {% for salon in salons %}
            <a href="{{ path('salon_edit', { 'id':salon.id }) }}" class="link_pink">Mon compte salon</a>
        {%endfor%}
    {% elseif is_granted('ROLE_USER') %}
        <a href=# class="link_pink">Mon compte</a>
    {% endif %}
    

    EDIT 1: Can you provide the Request Attributes section's content in the profiler

    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥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美术毛发渲染