尝试创建删除方法后,出现以下错误:
An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("id") to generate a URL for route "movie_delete".") in movies/index.html.twig at line 10.
我正在使用的代码:Twig模板:
{% extends 'base.html.twig' %}
{% block body %}
{% if movies|length == 0 %}
There are no movie items available. Add a movie <a href="{{ path('movie_create_form') }}">here</a> to get started.
{% elseif movies|length != 0 %}
These are the results: <br />
<ul>
{% for x in movies %}
<li>Title: {{ x.title }} - Price: {{ x.price }} - <a href="#">Edit</a> - <a href="{{ path('movie_delete') }}{{ x.id }}"> Delete</a></li>
{% endfor %}
</ul>
<a href="{{ path('movie_create_form') }}">Add more movie entries</a>
{% endif %}
{% endblock %}
删除课程:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use AppBundle\Entity\Movie;
class MovieDeleteController extends Controller
{
public function deleteAction($id)
{
$em = $this->getDoctrine()->getManager();
if(!$id)
{
throw $this->createNotFoundException('No ID found');
}
$movie = $this->getDoctrine()->getEntityManager()->getRepository('AppBundle:Movie')->Find($id);
if($movie != null)
{
$em->remove($movie);
$em->flush();
}
return $this->redirectToRoute('movies');
}
}
和我的routing.yml:
movie_delete:
path: /movies/delete/{id}
defaults: { _controller: AppBundle:MovieDelete:delete }
谁能解释我如何在Symfony中添加Delete方法,以便可以在上面编写的代码中应用更改?
您忘记了将'ID'变量传递到树枝路径。
<a href="{{ path('movie_delete') }}{{ x.id }}"> Delete</a>
应该
<a href="{{ path('movie_delete', {id: x.id}) }}"> Delete</a>
在“ movie_delete”路由中,您已经定义了“ id”参数。创建此路由需要此参数。在您的树枝文件中传递缺少的参数,您就完成了!
如果您也要沿“编辑”路线进行操作,请记住,您也将需要一个“ id”参数。确保像使用“删除”路线一样将其传递到树枝文件中。
请参阅:此Symfony文档,在此说明了细枝布线的其他参数。在您的情况下,“ slug”被替换为“ id”。
祝你好运!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句