它在这里说:控制器必须返回一个响应(给定null)。您是否忘记在控制器中的某处添加return语句?

时间:2013-05-20 06:56:32

标签: symfony

<?php
namespace Acme\TaskBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\TaskBundle\Entity\Task;
use Symfony\Component\HttpFoundation\Request;


class DefaultController extends Controller
{
  public function indexAction($name)
  {
    return $this->render('AcmeTaskBundle:Default:index.html.twig', array('name' => $name));
  }

  public function newAction(Request $request)
  { 
    // just setup a fresh $task object (remove the dummy data)
    $task = new Task();
    $task->setTask('Write a blog post');
    $task->setDueDate(new \DateTime('tomorrow'));

    $form = $this->createFormBuilder($task)
        ->add('task', 'text')
        ->add('dueDate', 'date')
        ->getForm();

    if ($request->isMethod('POST')) 
    {
      $form->bind($request);

      if ($form->isValid()) 
      {
        // perform some action, such as saving the task to the database

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

它在这里说:控制器必须返回响应(给定null)。您是否忘记在控制器中的某处添加return语句? 任何人都可以帮助我

1 个答案:

答案 0 :(得分:2)

如果if($request->isMethod('POST'))返回false,则您不做任何事情。因此,当您第一次创建表单时(并且您没有进入POST情况),您会遇到这种情况,并且必须向控制器操作调用者返回一些内容。

if ($request->isMethod('POST')) 
{
  $form->bind($request);

  if ($form->isValid()) 
  {
    // perform some action, such as saving the task to the database
    return $this->redirect($this->generateUrl('task_success'));
  }
}
// here, you have to return something (view,xml,redirect,etc...)