Response

The response represents the server'S HTTP response to the client. There are several different types listed below, each with an example of how the response from the controller action is generated.

String

Use the response service to return a simple string response.

public function indexAction()
{
    return $this['response']->create('My content');
}

Created View

Greencheap can create a view and send the response back for you. simply return an array with the $view key set to an array containing the title and name view.

All other parameters in the array are accessible from view. Learn more about views and templates.

public function indexAction($name = '')
{
    return [
        '$view' => [
            'title' => 'Hello World',
            'name' => 'hello:views/index.php',
        ],
        'name' => $name
    ];
}

If you don't want this to be a themed response, as described below, set $view' tolayout' = > false`.

Themed

A themed response embeds the result of the controller into a surrounding layout, usually defined by the theme. Simply return a string from the controller.

public function indexAction()
{
    return 'My content';
}

JSON

There are two ways to return a JSON response from the controller:

If the action returns an array or an object that implements \jsonserializable, a ' JsonResponse` is automatically generated.

public function jsonAction()
{
    return ['error' => true, 'message' => 'There is nothing here. Move along.'];
}

Of course, the response service can be used to achieve the same.

public function jsonAction()
{    
    return $this['response']->json(['error' => true, 'message' => 'There is nothing here. Move along.']);
}

orientation

Use a redirect response to redirect the user.

public function redirectAction()
{
    return $this['response']->redirect('@hello/greet/name', ['name' => 'Someone']);
}

Custom response and error pages

return any custom HTTP response using the create command.

public function forbiddenAction()
{
    return $this['response']->create('Permission denied.', 401);
}

Stream

Stream response allows content to be transferred to the client. Takes a callback function as the first argument. Within this callback, a flush call is sent directly to the customer.

public function streamAction()
{
    return $this['response']->stream(function() {
        echo 'Hello World';
        flush();
        echo 'Hello GreenCheap';
        flush();
    });
}

Download

Download response allows you to send a file to the client. Sets Content-Disposition: attachment to force the Save as dialog box in most browsers.

public function downloadAction()
{
    return $this['response']->download('extensions/hello/extension.svg');
}

Follow us on social media

We'll let you know about new announcements, publications and everything else on our social media accounts.

Contact us directly at support@greencheap.net

Greencheap logo

♥ GreenCheap is developed with love and caffeine.