2016-11-07 112 views
0

我的項目出現問題。 我試圖從實體中刪除一些數據時存在我的問題。我的控制器是用Sesio發生器生成的。這裏是我的代碼:Symfony2表單方法在Prod和Dev版本中不同

/** 
* 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() 
    ; 
} 

我必須告訴你,這個代碼工作的DEV(app_dev.php),但它不是在PROD版本工作不錯。

我嘗試解決這個問題,我試圖將表單方法更改爲POST,它的工作屬性od PROD和DEV。它看起來像DELETE方法在PROD版本中不起作用。

有人也有類似的問題?

+1

當您在PROD模式訪問你的應用程序,你需要清除高速緩存環境:'$ php bin/console cache:clear --env = prod'。你也許需要設置'var/cache','var/logs'和'var/sessions'目錄的權限:'$ sudo chmod 777 -R var/cache var/logs/var/sessions'前後的權限你使用'cache:clear'控制檯命令。然後試試看。 –

回答

0

如果您使用AppCache,內核將忽略爲DELETE方法添加的_method參數。

爲了解決您的web/app.php電話Request::enableHttpMethodParameterOverride();問題創建請求對象之前:

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

http://symfony.com/doc/current/form/action_method.htmlhttp://symfony.com/doc/current/reference/configuration/framework.html#configuration-framework-http-method-override

相關問題