/**
* @Route("/DeletePoll/{pollId}",name="poll_delete")
* @Template()
*/
public function DeletePollAction($pollId)
{
$entita = $this->get('doctrine')->getRepository('CvutFitBiWt1PollBundle:Poll')->find($pollId);
$em = $this->getDoctrine()->getManager();
$em->remove($entita);
$em->flush();
return $this->render('CvutFitBiWt1PollBundle:Poll:index.html.twig');
}
在我的index.html.twig
中{% if polls %}
{% for poll in polls %}
<a href="{{ path('poll_delete',{'pollId': poll.id}) }}">Delete</a>
它写错误:
变量“民意调查”不存在于 CvutFitBiWt1PollBundle:民意调查:index.html.twig at line 15 500 Internal 服务器错误 - Twig_Error_Runtime
在routing.yml
中cvut_fit_bi_wt1_poll:
resource: "@CvutFitBiWt1PollBundle/Controller/"
type: annotation
prefix: /
我不知道,怎么写“返回”。
现在它的工作。返回必须像
return $this->redirect($this->generateUrl('poll_index'));
poll_index来自
/**
* @Route("/",name="poll_index")
* @Template()
*/
public function indexAction() {}
答案 0 :(得分:1)
您不会在此处向模板传递任何polls
变量:
return $this->render('CvutFitBiWt1PollBundle:Poll:index.html.twig', array('pollId' => $pollId));
您需要指定它以在模板中使用它。
答案 1 :(得分:0)
使用此路由模式进行删除
poll_delete:
pattern: /DeletePoll/{pollId}
defaults: { _controller: "CvutFitBiWt1PollBundle:Poll:DeletePollAction" }
requirements: { _method: get|delete }
在树枝模板中调用
<a class="btn btn-danger" href="{{ path('poll_delete', { 'pollId': pollId }) }}">
使用重定向删除控制器
public function DeletePollAction($pollId)
{
$entity = $this->get('doctrine')->getRepository('CvutFitBiWt1PollBundle:Poll')->find($pollId);
$em = $this->getDoctrine()->getManager();
$em->remove($entity);
$em->flush();
return $this->redirect($this->generateUrl('poll_index'));
}