英文:
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("/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"],
],
]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论