2013-08-05 44 views
0

我有這樣的代碼在我的控制器:控制器必須返回響應,動作永遠不會執行

/** 
* Displays a form to create a new Bank Account 
* 
* @Route("/account/new", name="wba_new") 
* @Method("GET") 
* @Template("BankBundle:BankAccount:new.html.twig") 
*/ 
public function newBankAccountAction() { 
    $entity = new Account(); 
    $form = $this->createForm(new AccountType(), $entity); 

    return array('entity' => $entity, 'form' => $form->createView()); 
} 

/** 
* Handle bank account creation 
* 
* @Route("/", name="wba_create") 
* @Method("POST") 
*/ 
public function createAction(Request $request) { 
    $entity = new Account(); 
    $form = $this->createForm(new AccountType(), $entity); 
    $form->handleRequest($request); 

    print_r($request); 
    exit; 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($entity); 
     $em->flush(); 

     return $this->redirect($this->generateUrl('wba_list')); 
    } 

    return array('entity' => $entity, 'form' => $form->createView()); 
} 

當我打電話/account/new形式表現沒有任何問題,並採取行動去/但是當我送我得到這個錯誤:

The controller must return a response (Array(entity => Object(BankBundle\Entity\AccountType), form => Object(Symfony\Component\Form\FormView)) given).

爲什麼?我的代碼有什麼問題?

UPDATE

我發現那裏的問題是,我有相同的定義兩條路線在兩個不同的控制器:

/** 
* Handle bank account creation 
* 
* @Route("/", name="wba_create") 
* @Method("POST") 
*/ 

後解決問題的東西工程

+1

是不是控制器操作應該返回某種'Response'對象? –

+0

@JoachimIsaksson不總是,看我的版 – Reynier

+1

@JoachimIsaksson是對的。您在'createAction()'函數的頂部忘了'@ Template'註釋。 – cheesemacfly

回答

0

讀之後代碼再次完成,並試圖找到我的錯誤,最後我發現。我有兩個控制器:AccountController.phpTestController.php和兩個我已經定義(我的錯誤,因爲我只是複製AccountController.phpTestController.php)相同的路線在此功能:

/** 
* Handle bank account creation 
* 
* @Route("/", name="wba_create") 
* @Method("POST") 
*/ 
public function createAction(Request $request) { 
    ... 
} 

出於這個原因,我艱難的,是爲什麼當Symfony嘗試呼叫路線wba_create時,數據丟失。我沒有添加註釋@Template("")。這是解決方案,希望適用於任何運行相同的問題

0
/** 
* Displays a form to create a new Bank Account 
* 
* @Route("/account/new", name="wba_new") 
*/ 
public function newBankAccountAction() 
{ 
    $entity = new Account(); 
    $form = $this->createForm(new AccountType(), $entity); 

    return $this->render('BankBundle:BankAccount:new.html.twig',array(
      'entity' => $entity, 
      'form' => $form->createView(), 
    )); 
} 
相關問題