symfony2

Response

Parameters#

Parameter Details
string content The response content.
integer status The HTTP status code.
array headers Array of response headers.
## Simple usage
public function someAction(){
        
    // Action's code 
        
    return new Response('<span>Hello world</span>'); 
}

Set status code

public function someAction(){
    
    // Action's code 

    return new Response($error, 500); 
}

Another example:

public function someAction(){
    
    // Action's code 

    $response = new Response(); 
    $response->setStatusCode(500)
    $response->setContent($content); 
    return $response;
}

Set header

See list of http headers.

 public function someAction(){
    
    // Action's code
    $response = new Response(); 

    $response->headers->set('Content-Type', 'text/html');

    return $response; 
}

JsonResponse

Return JSON formated response:

use Symfony\Component\HttpFoundation\JsonResponse;

public function someAction(){
    
    // Action's code

    $data = array(
        // Array data
    );

    return new JsonResponse($data); 
}

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow