0

I don't know why im getting this error every time when I wanna save a new model into my collection. My route is matched but the error appears everytime. I have a table with id, title, document and user_id. I just wanna save my title. Can anyone help me?

Error: Failed to start the session because headers have already been sent by "" at line 0.

PHP code:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use AppBundle\Entity\Document;
use Doctrine\ORM\Mapping as ORM;

class DocumentQueryController extends Controller
{  
    public function documentAction()
    {
        $id = $this->getUser()->getId();

    $em = $this->getDoctrine()->getManager();
    $documents = $em->getRepository('AppBundle:Document')->findAllOrderedById();
    $titles = array();

    foreach($documents as $document) 
    {
        $user_id = $document['user_id'];
        $title = $document['title'];

        if($user_id == $id)
        {
            $titles[] = $document;
        }             
    }

    return new JsonResponse($titles);

    //return new JsonResponse($document);
    //return new JsonResponse($documents);
    //dump(json_encode($documents));
    //return $this->render('default/doc.html.twig', array('documents' => $documents, 'id' => $id));
}

public function createAction(Request $request)
{
    $content = $this->get("request")->getContent();
    $json = json_decode($content);

    $id = $this->getUser()->getId();

    $document = new Document();
    $user_id = $id;

    $document->setTitle($request->get('title'));
    $document->setUserId($user_id);
    //$document->setBody($content);

    $em = $this->getDoctrine()->getManager();
    $em->persist($document);
    $em->flush();

    return new JsonResponse($document);
}
}
lorenos
  • 115
  • 2
  • 12
  • A little more debug information would help. Does it only happen a particular method? Is there any middleware? It doesn't look like you are prematurely setting any headers in the code you posted. – SArnab Jul 04 '16 at 12:49
  • It's a POST method for that route for saving my model. I have tried this "always_populate_raw_post_data' to '-1' in php.ini" but it didn't worked. Could it be a problem in loaded php? – lorenos Jul 04 '16 at 12:52
  • Try this, hope it should help you `$response = new JsonResponse($result); $response->headers->set('Content-Type', 'application/json'); return $response;` – Ashutosh Rai Jul 04 '16 at 12:55
  • Where should i put it? – lorenos Jul 04 '16 at 12:59
  • @AshutoshRai I'm fairly certain JsonResponse automatically sets the content type. – SArnab Jul 04 '16 at 13:10
  • It didn't help me. – lorenos Jul 04 '16 at 13:12
  • @lorenos put it or replace with `return new JsonResponse($titles);` in your code. @SArnab i have used this in many applications and working proper, also checked for this is the standard way to give response in SF2 – Ashutosh Rai Jul 04 '16 at 13:13
  • @AshutoshRai the problem is in the second method createAction(), not in the first – lorenos Jul 04 '16 at 13:23
  • You can visit here for json response in SF [link](http://symfony.com/doc/current/components/http_foundation/introduction.html#creating-a-json-response) one more thing why not using $request to get content like `$content = $request->getContent();` – Ashutosh Rai Jul 04 '16 at 14:18

0 Answers0