Response

Other topics

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); 
}

Parameters:

ParameterDetails
string contentThe response content.
integer statusThe HTTP status code.
array headersArray of response headers.

Contributors

Topic Id: 9218

Example Ids: 28608,28609,28610,28611

This site is not affiliated with any of the contributors.