1
<?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'));
      }
    }
  }
}

它在这里说:控制器必须返回一个响应(给定空值)。您是否忘记在控制器的某处添加 return 语句?谁可以帮我这个事

4

1 回答 1

1

如果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...)
于 2013-05-20T07:02:31.383 回答