Haz click en cada una de las respuestas para ver el resultado.

  • Response
    public function responseAction()
    {
        return new Response('Respuesta simple!');
    }
  • ResponseRedirect
    public function redirectAction()
    {
        return new RedirectResponse($this->generateUrl('examples_responses'));
    }
  • JsonResponse
    public function jsonResponseAction()
    {
        return new JsonResponse(array(
            'class' => '\Symfony\Component\HttpFoundation\JsonResponse',
            'type' => 'json'
        ));
    }
  • JsonResponse (JSONP)
    public function jsonpResponseAction()
    {
        $response = new JsonResponse(array(
            'class' => '\Symfony\Component\HttpFoundation\JsonResponse',
            'type' => 'jsonp'
        ));
        $response->setCallback('handleData');
    
        return $response;
    }
  • StreamedResponse
    public function streamedResponseAction()
    {
        $response = new StreamedResponse();
        $response->setCallback(function () {
            echo 'Loading';
            flush();
            ob_flush();
            for ($i=0; $i < 10; $i++) {
                usleep(200000);
                echo '.';
                flush();
                ob_flush();
            }
            echo 'Completed!';
            flush();
            ob_flush();
        });
    
        return $response;
    }

Ir al artículo

Controller Code - Github

    /**
     * @Route("/", name="examples_responses")
     * @Template()
     */
    public function responsesAction()
    {
        return array();
    }

Template Code - Github

{% extends 'MainBundle::layout.html.twig' %}

{% import 'MainBundle:Macros:breadcrumb.html.twig' as breadcrumb %}

{% set section = 'examples' %}

{% block breadcrumb %}
{{ breadcrumb.show_example('Respuestas') }}
{% endblock %}

{% block content %}
    <div class="row">
        <div class="col-md-12">
            <p>Haz click en cada una de las respuestas para ver el resultado.</p>
            <ul class="list-group">
                <li class="list-group-item"><a href="{{ path('responses_response') }}">Response</a>
                    <pre class="prettyprint">
public function responseAction()
{
    return new Response('Respuesta simple!');
}</pre>
                </li>
                <li class="list-group-item">
                    <a href="{{ path('responses_redirect') }}">ResponseRedirect</a>
                    <pre class="prettyprint">
public function redirectAction()
{
    return new RedirectResponse($this->generateUrl('examples_responses'));
}</pre>
                </li>
                <li class="list-group-item">
                    <a href="{{ path('responses_json') }}">JsonResponse</a>
                    <pre class="prettyprint">
public function jsonResponseAction()
{
    return new JsonResponse(array(
        'class' => '\Symfony\Component\HttpFoundation\JsonResponse',
        'type' => 'json'
    ));
}</pre>
                </li>
                <li class="list-group-item">
                    <a href="{{ path('responses_jsonp') }}">JsonResponse (JSONP)</a>
                    <pre class="prettyprint">
public function jsonpResponseAction()
{
    $response = new JsonResponse(array(
        'class' => '\Symfony\Component\HttpFoundation\JsonResponse',
        'type' => 'jsonp'
    ));
    $response->setCallback('handleData');

    return $response;
}</pre>
                </li>
                <li class="list-group-item">
                    <a href="{{ path('responses_streamed') }}">StreamedResponse</a>
                    <pre class="prettyprint">
public function streamedResponseAction()
{
    $response = new StreamedResponse();
    $response->setCallback(function () {
        echo 'Loading';
        flush();
        ob_flush();
        for ($i=0; $i < 10; $i++) {
            usleep(200000);
            echo '.';
            flush();
            ob_flush();
        }
        echo 'Completed!';
        flush();
        ob_flush();
    });

    return $response;
}</pre>
                </li>
            </ul>
        </div>
    </div>
{% endblock %}


{% set article_link = 'http://showmethecode.es/php/symfony/symfony2-response-responseredirect-jsonresponse-y-streamedresponse/' %}

Fork me on GitHub