Aize
Aize

Reputation: 546

The controller must return a response 500 Internal Server Error - LogicException

Can't find a way to solve this issue since one week :'(

The controller must return a response (Array(order => Object(Bull\LogisticBundle\Entity\Order), form => Object(Symfony\Component\Form\FormView), postRoute => /taz/web/app_dev.php/fr/spareordered/create/1) given). 500 Internal Server Error - LogicException

Here my controller :

namespace Taz\LogisticBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Taz\LogisticBundle\Entity\SparepartOrdered;
use Taz\LogisticBundle\Form\SparepartOrderedType;

/**
 * SparepartOrderedController controller.
 *
 * @Route("/spareordered")
 *
 * @SuppressWarnings(PHPMD)
 */
class SparepartOrderedController extends LogisticController
{



     const CONTEXT = 'SparepartOrdered';

        /**
         * Return the new SparepartOrdered form.
         *
         * @param int $orderId the order id
         *
         * @return array view parameters
         *
         * @Route("/new/{orderId}", name="sparepartordered_new")
         */
        public function newAction($orderId)
        {
            $this->writeLog();
            $order = $this->getOrderRepository()->find($orderId);

            if (!$order) {
                throw $this->createNotFoundException('Unable to find Order entity.');
            }

            $sparepartOrdered = new SparepartOrdered();
            $form = $this->createSparepartOrderedForm($sparepartOrdered);

            $postRoute = $this->generateUrl(
                'sparepartordered_create',
                array('orderId' => $order->getId())
            );

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

/**
     * Creates a new SparepartOrdered entity.
     *
     * @param Request $request    the request
     * @param int     $orderId    the order id
     *
     * @return array view parameters
     *
     * @Route("/create/{orderId}", name="sparepartordered_create")
     * @Method("POST")
     * @Template("TazLogisticBundle:SparepartOrdered:new.html.twig")
     */
    public function createSparepartAction(Request $request, $orderId)
    {
        $this->writeLog();
        $order = $this->getOrderRepository()->find($orderId);

        if (!$order) {
            throw $this->createNotFoundException(
                'Unable to find Order entity.'
            );
        }

        $sparepartOrdered = new SparepartOrdered();
        $form = $this->createSparepartOrderedForm($sparepartOrdered);
        $form->handleRequest($request);

        $sparepartOrdered->setOrder($order);

        if ($form->isValid()) {

            $eManager = $this->getDoctrine()->getManager();
            $eManager->persist($sparepartOrdered);
            $eManager->persist($order);
            $eManager->flush();

            $this->setFlashNotice('success.sparepartordered.created');

            return $this->redirect(
                    $this->generateUrl('order_edit',
                                       array('entityId' => $order->getId()))
            );
        } else {
            $postRoute = $this->generateUrl(
                'sparepartordered_create',
                array('orderId' => $order->getId())
            );

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

    /**
     * Create the SparepartOrdered Form
     *
     * @param type $entity the SparepartOrdered to map to the form
     *
     * @return SparepartOrderedType the form
     */
    private function createSparepartOrderedForm($entity)
    {
        $this->writeLog();

        return $this->createForm(
                new SparepartOrderedType(), $entity,
                array(
                'em'           => $this->getDoctrine()->getManager()
                )
        );
    }

My entities and repositories works perfectly. All workaround founded on this subject do not resolve this issue.

Thanks for ur help !

Regards,

Upvotes: 0

Views: 763

Answers (1)

Raphaël Malié
Raphaël Malié

Reputation: 4002

The error is explicit : the controller must return a response.

If you want to return only an array of parameters, and taking advantage of the @template annotation, you must add it in the comment of the newAction method, the same way you did with other actions.

Upvotes: 1

Related Questions