Laravel Responses: Creating Responses

Strings & Arrays

Route::get('/', function () {
    return 'Hello World';
Route::get('/', function () {
    return [1, 2, 3];

In addition to returning strings from your routes and controllers, you may also return arrays.

Response Objects

Route::get('home', function () {
    return response('Hello World', 200)
                  ->header('Content-Type', 'text/plain');

Returning a full Response instance allows you to customize the response's HTTP status code and headers.

Attaching Headers To Responses

Return response($content)
            ->header('Content-Type', $type)
            ->header('X-Header-One', 'Header Value')
            ->header('X-Header-Two', 'Header Value');
Return response($content)
                'Content-Type' => $type,
                'X-Header-One' => 'Header Value',
                'X-Header-Two' => 'Header Value',

You may use the header method to add a series of headers to the response before sending it back to the user.

You may use the withHeaders method to specify an array of headers to be added to the response.

Cookies & Encryption

 * The names of the cookies that should not be encrypted.
 * @var array
protected $except = [

By default, all cookies generated by Laravel are encrypted and signed so that they can't be modified or read by the client.

Laravel Responses: Creating Responses — Structure map

Clickable & Draggable!

Laravel Responses: Creating Responses — Related pages: