Symfony my Controller::updateAction() 要求您为 “$name” 参数提供一个值


Symfony my Controller::updateAction() requires that you provide a value for the "$name" argument

使用Symfony 2,当我尝试传递参数/变量时,我遇到了此错误:

Controller "MyBundle'Controller'ManageController::updateAction()" 
requires that you provide a value for the "$name" argument (because there is 
no default value or because there is a non optional argument after this one).

这是实体/模型Parcs

namespace MyBundle'DatabaseBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
 * Parcsimmobilier
 *
 * @ORM'Table(name="parcs")
 * @ORM'Entity
 */
class Parcs
{
    /**
     * @var integer
     *
     * @ORM'Column(name="id", type="integer", nullable=false)
     * @ORM'Id
     * @ORM'GeneratedValue(strategy="IDENTITY")
     */
    private $id;
    /**
     * @var string
     *
     * @ORM'Column(name="name", type="string", length=150, nullable=false)
     */
    private $name;
首先,我

有一个树枝,它可以允许用户修改/更新 Parc,我正在使用 JavaScript 允许 html 中的select tag在做出选择时在另一个路由/树枝中显示表单,这是选择用户想要更新的 parc 的代码:

{% block content %}
<div class="form-group">
  <label class="control-label">Which Parcs would you like to update ?</label>
  <form action="{{ path('UpdateParcs_form' , {'name':'parcs.name'}) }}" method="GET">
    <select class="form-control" id="selectpicker">
      <option disabled selected> select your parc </option>
      {% for parcsimmobilier in parc %}
        <option value="parc"><strong>{{ parcs.name}}</strong></option>
      {% endfor %}
    </select>
  </form>
</div>
{% endblock %}

这是JavaScript代码,它允许我在显示更新表单的其他路由上选择后重定向用户:

$(function() {
    $('#selectpicker').change(function() {
        this.form.submit();
    });
});

现在,向显示表单以更新所选公园的树枝:

{% block content %}
    <form action="{{ path('updateParcs_process') }}" method="POST">
      <div>
      {{ form_label(form.name, "change the name of the parc", {'label_attr': {'class': 'control-label'}}) }}
        <div>
          {{ form_widget(form.name, {'attr': {'class': 'form-control'}}) }}
        </div>
      </div>
      <input type="submit" value="Update" class="btn btn-success"/>
    </form>
{% endblock %}

因此,当我在更新按钮上提交时,出现错误:

控制器"MyBundle''Controller''ManageController::updateAction(("要求您为"$name"参数提供一个值(因为没有默认值或在此参数之后有一个非可选参数(。

这是我的控制器代码和更新操作

public function updateAction($name) {
    $em = $this->getDoctrine()->getManager();
    $parc = $em->getRepository('MySpaceDatabaseBundle:Parcs')->find($name);
    $form = $this->createForm(new ParcsType(), $parc);
    $request = $this->get('request');
    if ($request->isMethod('POST') | ($form->isValid())) {
            $form->bind($request);
            $parc = $form->getData();
            $em->flush();
            return $this->redirect($this->generateUrl('index_parcs'));
        }
    //retourner le formulaire d'ajout si c'est invalide
    else {
            return $this->render('MySpaceManageBundle:Parcs:updateParcs.html.twig', array('form' => $form->createView(), 'parc' => $parc, 'name' => $name));
         }
}

这是我的路由的路由文件:

# index for management
index_Parcs:
    path:     /manageparcs
    defaults: { _controller: MySpaceManageBundle:Manage:indexParcs }
    requirements:
    methods: GET
# route for display the form after the select choice
updateParcs_form:
    path:     /manageparcs/update/form/{name}
    defaults: { _controller: MySpaceManageBundle:Manage:update }
    requirements:
    methods: GET
# route for processing form with update
updateParcs_process:
    path:     /manageparcs/update/form/success
    defaults: { _controller: MySpaceManageBundle:Manage:update }
    requirements:
    methods: POST

我的问题是这些:为什么会出现此错误?我该如何解决它?以及如何使用此更新操作((正确更新我的实体/模型。我怎样才能通过 Symfony 中的路由传递参数

updateParcs_form:
    path:     /manageparcs/update/form/{name}
    defaults: { _controller: MySpaceManageBundle:Manage:update }
    requirements:
    methods: GET
# route for processing form with update
updateParcs_process:
    path:     /manageparcs/update/form/success
    defaults: { _controller: MySpaceManageBundle:Manage:update }
    requirements:
    methods: POST

第二个路由不像第一个路由那样指定 name 参数。
原因是该方法需要参数,但基于此配置,不会提供任何参数。

除非要为 POST 请求创建另一个操作,否则需要将名称添加到 POST URI。

path:     /manageparcs/update/form/{name}/success

您将需要使用此参数生成 URI。

您正在做正确的事情,除了您在控制器中将$nom作为参数,但在路由配置中name。根据其他文件,只需更改配置即可轻松:

routing.yml中的name更改为nom

updateParcs_form:
path:     /manageparcs/update/form/{nom}
defaults: { _controller: MySpaceManageBundle:Manage:update }
requirements:
methods: GET