Reputation: 15393
Building a Symfony 2 application and I am in CategoryController.php. I have developed the create, read, update, and when I get to delete is when I start getting this error:
The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?
Here is the whole file for 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');
}
}
Upvotes: 0
Views: 60
Reputation: 626
We need more info, is the url of this error the route of category_list or is it the route of category delete? It is possible that you are redirecting successfully but category_list is not returning a valid response object. I think you have a problem in the category_list controller.
Upvotes: 1
Reputation: 646
Clear your cache using php app/console cache:clear
return $this->redirect($this->generateUrl('category_list'));
Upvotes: 1