Symfony/Twig中根据角色渲染数据的问题

huangapple go评论50阅读模式
英文:

Issue with rendering data based on role in Symfony/Twig

问题

> 你好,
> 
> 我目前正在开发一个Symfony项目,需要根据用户的角色来渲染“mededelingens”(通知)列表。我有两个路由,一个用于“leerling”(学生),一个用于“instructeur”(教练),我正在使用findBy方法从MededelingenRepository中检索适当的数据。
> 
> 但是,我遇到了一个问题,Twig模板中的循环没有显示任何数据。
> 我怀疑与将“rol”参数传递给findBy方法相关的代码可能存在错误。
英文:

> Hello,
>
> I'm currently working on a Symfony project where I need to render a list of "mededelingens" (announcements) based on the user's role. I have two routes, one for "leerling" (student) and one for "instructeur" (instructor), and I'm using the findBy method to retrieve the appropriate data from the MededelingenRepository.
>
> However, I'm encountering an issue where the loop in the Twig template does not display any data.
> I suspect there might be an error in the code related to passing the "rol" parameter to the findBy method.
>

/**
 * @Route("/leerling/mededelingen", name="app_leerling")
 */
public function show(MededelingenRepository $mededelingenRepository) {
    $mededelingen = $mededelingenRepository->findBy(["rol" => '["ROLE_LEERLING"]']);

    return $this->render('leerling/mededelingen.html.twig',
        ['mededelingen' => $mededelingen]
    );
}

/**
 * @Route("/instructeur/mededelingen", name="app_instructeur")
 */
public function show(MededelingenRepository $mededelingenRepository) {
    $mededelingen = $mededelingenRepository->findBy(["rol" => '["ROLE_INSTRUCTEUR"]']);

    return $this->render('instructeur/mededelingen.html.twig',
        ['mededelingen' => $mededelingen]
    );
}

{% for mededelingens in mededelingen %}
    <p>{{ mededelingens.name }}</p>
{% endfor %}

$builder->add('rol', ChoiceType::class, [
    'choices'  => [
        '["ROLE_LEERLING"]' => ["ROLE_LEERLING"],
        '["ROLE_INSTRUCTEUR"]' => ["ROLE_INSTRUCTEUR"],
    ],
]);

答案1

得分: 0

Your code has been rewritten.

/**
 * @Route("/student/messages", name="app_student")
 */
public function show(MessagesRepository $messagesRepository) {
    $messages = $messagesRepository->findBy(["role" => '["ROLE_STUDENT"]']);

    return $this->render('student/messages.html.twig',
        ['messages' => $messages,
    ]);
}

/**
 * @Route("/instructor/messages", name="app_instructor")
 */
public function show(MessagesRepository $messagesRepository) {
    $messages = $messagesRepository->findBy(["role" => '["ROLE_INSTRUCTOR"]']);

    return $this->render('instructor/messages.html.twig',
        ['messages' => $messages,
    ]);
}

{% for message in messages %}

<p>{{ message.name }}</p>

{% endfor %}

$builder->add('role', ChoiceType::class, [
    'choices'  => [
        '["ROLE_STUDENT"]' => ["ROLE_STUDENT"],
        '["ROLE_INSTRUCTOR"]' => ["ROLE_INSTRUCTOR"],
    ],
]);

Please note that I've translated "Mededelingen" to "Messages" and "leerling" to "student" and "instructeur" to "instructor" for clarity.

英文:

Your code has been rewritten .

/**
     * @Route(&quot;/leerling/mededelingen&quot;, name=&quot;app_leerling&quot;)
     */ public function show(MededelingenRepository $mededelingenRepository) {
        $mededelingen = $mededelingenRepository-&gt;findBy([&quot;rol&quot; =&gt; &#39;[&quot;ROLE_LEERLING&quot;]&#39;]);

        return $this-&gt;render(&#39;leerling/mededelingen.html.twig&#39;,
            [ &#39;mededelingen&#39; =&gt; $mededelingen,
        ]);
    }



/**
     * @Route(&quot;/instructeur/mededelingen&quot;, name=&quot;app_instructeur&quot;)
     */ public function show(MededelingenRepository $mededelingenRepository) {
        $mededelingen = $mededelingenRepository-&gt;findBy([&quot;rol&quot; =&gt; &#39;[&quot;ROLE_INSTRUCTEUR&quot;]&#39;]);

        return $this-&gt;render(&#39;instructeur/mededelingen.html.twig&#39;,
            [ &#39;mededelingen&#39; =&gt; $mededelingen,
        ]);
    }
    
    {% for mededelingens in mededelingen %}

    &lt;p&gt; {{ mededelingens.name }} &lt;/p&gt;

    {% endfor %}
    
    
    
    $builder-&gt;add(&#39;rol&#39;, ChoiceType::class, [
                &#39;choices&#39;  =&gt; [
                    &#39;[&quot;ROLE_LEERLING&quot;]&#39; =&gt; [&quot;ROLE_LEERLING&quot;],
                    &#39;[&quot;ROLE_INSTRUCTEUR&quot;]&#39; =&gt; [&quot;ROLE_INSTRUCTEUR&quot;],
                ],
            ]);

huangapple
  • 本文由 发表于 2023年5月11日 05:43:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222744.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定