有谁知道为什么我继续得到这个错误?

时间:2017-01-17 06:39:41

标签: php symfony

构建Symfony 2应用程序,我在CategoryController.php中。我已经开发了创建,读取,更新,当我开始删除时,我开始收到此错误:

The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?

以下是CategoryController.php的整个文件:

<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Category;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class CategoryController extends Controller
{
    /**
     * @Route("/categories", name="category_list")
     */
    public function indexAction(Request $request)
    {
        $categories = $this->getDoctrine()->getRepository('AppBundle:Category')->findAll();

        // Render Template
        return $this->render('category/index.html.twig', array(
            'categories' => $categories
        ));
    }

    /**
     * @Route("/category/create", name="category_create")
     */
    public function createAction(Request $request)
    {
       $category = new Category;

       $form = $this->createFormBuilder($category)->add('name', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))->add('save', SubmitType::class, array('label' => 'Create Category', 'attr' => array('class' => 'btn btn-primary')))->getForm();

       // Handle Request
       $form->handleRequest($request);

       // Check Submit
       if($form->isSubmitted() && $form->isValid()){
        $name = $form['name']->getData();

        // Get Current Date and Time
        $now = new \DateTime("now");

        $category->setName($name);
        $category->setCreateDate($now);

        $em = $this->getDoctrine()->getManager();

        $em->persist($category);
        $em->flush();

        $this->addFlash(
            'notice',
            'Category Saved'
        );

        return $this->redirectToRoute('category_list');
       }

       // Render Template
        return $this->render('category/create.html.twig', array(
            'form' => $form->createView()
        ));
    }

    /**
     * @Route("/category/edit/{id}", name="category_edit")
     */
    public function editAction($id, Request $request)
    {
        $category = $this->getDoctrine()->getRepository('AppBundle:Category')->find($id);

        if(!$category){
            throw $this->createNotFoundException(
                'No category found for id '.$id
            );
        }

        $category->setName($category->getName());

       $form = $this->createFormBuilder($category)->add('name', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))->add('save', SubmitType::class, array('label' => 'Create Category', 'attr' => array('class' => 'btn btn-primary')))->getForm();

       // Handle Request
       $form->handleRequest($request);

       // Check Submit
       if($form->isSubmitted() && $form->isValid()){
        $name = $form['name']->getData();

        $em = $this->getDoctrine()->getManager();
        $category = $em->getRepository('AppBundle:Category')->find($id);

        $category->setName($name);
        $em->flush();

        $this->addFlash(
            'notice',
            'Category Updated'
        );

        return $this->redirectToRoute('category_list');
       }

       // Render Template
        return $this->render('category/edit.html.twig', array(
            'form' => $form->createView()
        ));
    }

    /**
     * @Route("/category/delete/{id}", name="category_delete")
     */
    public function deleteAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $category = $em->getRepository('AppBundle:Category')->find($id);

        if(!$category){
            throw $this->createNotFoundException(
                'No category found with id of '.$id
            );
        }

        $em->remove($category);
        $em->flush();

        $this->addFlash(
            'notice',
            'Category Deleted'
        );

        return $this->redirectToRoute('category_list');
    }
}

2 个答案:

答案 0 :(得分:1)

使用php app / console cache清除缓存:清除

return $this->redirect($this->generateUrl('category_list'));

答案 1 :(得分:1)

我们需要更多的信息,这个错误的网址是category_list的路由还是类别删除的路由? 您可能正在成功重定向,但category_list未返回有效的响应对象。我认为你在category_list控制器中遇到了问题。