29.07.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

HTTP Responses 93<br />

.<br />

1 Route::get('home', function () {<br />

2 return response($content, $status)<br />

3 ->header('Content-Type', $value);<br />

4 });<br />

Note: For a full list of available Response methods, check out its API documentation⁴⁹ and<br />

the Symfony API documentation⁵⁰.<br />

Attaching Headers To Responses<br />

Keep in mind that most response methods are chainable, allowing for the fluent building of<br />

responses. For example, you may use the header method to add a series of headers to the response<br />

before sending it back to the user:<br />

.<br />

1 return response($content)<br />

2 ->header('Content-Type', $type)<br />

3 ->header('X-Header-One', 'Header Value')<br />

4 ->header('X-Header-Two', 'Header Value');<br />

Attaching Cookies To Responses<br />

The withCookie helper method on the response instance allows you to easily attach cookies to the<br />

response. For example, you may use the withCookie method to generate a cookie and attach it to<br />

the response instance:<br />

.<br />

1 return response($content)->header('Content-Type', $type)<br />

2 ->withCookie('name', 'value');<br />

The withCookie method accepts additional optional arguments which allow you to further customize<br />

your cookie’s properties:<br />

⁴⁹http://<strong>laravel</strong>.com/api/master/Illuminate/Http/Response.html<br />

⁵⁰http://api.symfony.com/2.7/Symfony/Component/HttpFoundation/Response.html

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!