简体   繁体   English

Prof和Dev版本中的Symfony2表单方法不同

[英]Symfony2 form methods diffrent in Prod and Dev version

I have problem with my project. 我的项目有问题。 My problem exist when I try to delete some data from entity. 当我尝试从实体中删除一些数据时,我的问题就存在了。 My controller was generated with Sesio generator. 我的控制器是用Sesio生成器生成的。 Here is my code: 这是我的代码:

/**
 * Deletes
 * @Route("/{id}/delete", name="delete")
 * @Method({"DELETE"})
 */
public function deleteAction(Request $request, Task $task) {

    $form = $this->createDeleteForm($task);
    $form->handleRequest($request);


    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->remove($task);
        $em->flush();
        $this->get('session')->getFlashBag()->add('notice_success', 'Success');
    } else {
        $this->get('session')->getFlashBag()->add('notice_error', 'NO DELETE');
    }
    return $this->redirectToRoute('task');
}

/**
 * Creates a form to delete.
 */
private function createDeleteForm(Task $task) {
    return $this->createFormBuilder()
                    ->setAction($this->generateUrl('delete', array('id' => $task->getId())))

                    ->add('submit', \Symfony\Component\Form\Extension\Core\Type\SubmitType::class, array('label' => 'Delete'))
                    ->getForm()
    ;
}

I have to tell you that this code work nice on DEV (app_dev.php) but It isn't working in PROD version. 我必须告诉您,此代码在DEV(app_dev.php)上运行良好,但在PROD版本中不起作用。

I try to solve that problem and I have tried to change form method to POST and it work property od PROD and DEV. 我尝试解决该问题,并且尝试将表单方法更改为POST,并且它可以工作于od PROD和DEV属性。 It look like DELETE method doesnt work in PROD version. 看来DELETE方法在PROD版本中不起作用。

Someone have similar problem? 有人有类似的问题吗?

If you're using the AppCache, the kernel will ignore the _method parameter added for DELETE method. 如果您使用的是AppCache,则内核将忽略为DELETE方法添加的_method参数。

To solve the problem in your web/app.php call Request::enableHttpMethodParameterOverride(); 要解决您的web/app.php的问题,请调用Request::enableHttpMethodParameterOverride(); before creating the Request object: 在创建Request对象之前:

...
$kernel = new AppCache($kernel);
Request::enableHttpMethodParameterOverride();// <-- add this line
$request = Request::createFromGlobals();
...

See http://symfony.com/doc/current/form/action_method.html and http://symfony.com/doc/current/reference/configuration/framework.html#configuration-framework-http-method-override 请参阅http://symfony.com/doc/current/form/action_method.htmlhttp://symfony.com/doc/current/reference/configuration/framework.html#configuration-framework-http-method-override

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM