29.07.2016 Views

laravel-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

HTTP Responses<br />

• Basic Responses A> - Attaching Headers To Responses A> - Attaching Cookies To Responses<br />

• Other Response Types A> - View Responses A> - JSON Responses A> - File Downloads<br />

• Redirects A> - Redirecting To Named Routes A> - Redirecting To Controller Actions A> -<br />

Redirecting With Flashed Session Data<br />

• Response Macros<br />

Basic Responses<br />

Of course, all routes and controllers should return some kind of response to be sent back to the<br />

user’s browser. Laravel provides several different ways to return responses. The most basic response<br />

is simply returning a string from a route or controller:<br />

.<br />

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

2 return 'Hello World';<br />

3 });<br />

The given string will automatically be converted into an HTTP response by the framework.<br />

However, for most routes and controller actions, you will be returning a full Illuminate\Http\Response<br />

instance or a view. Returning a full Response instance allows you to customize the response’s HTTP<br />

status code and headers. A Response instance inherits from the Symfony\Component\HttpFoundation\Response<br />

class, providing a variety of methods for building HTTP responses:<br />

.<br />

1 use Illuminate\Http\Response;<br />

2<br />

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

4 return (new Response($content, $status))<br />

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

6 });<br />

For convenience, you may also use the response helper:

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

Saved successfully!

Ooh no, something went wrong!