06.10.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 161<br />

1 return response()<br />

2 ->view('hello', $data)<br />

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

Of course, if you do not need to pass a custom HTTP status code or custom headers, you should<br />

simply use the global view helper function.<br />

JSON Responses<br />

The json method will automatically set the Content-Type header to application/json, as well as<br />

convert the given array into JSON using the json_encode PHP function:<br />

1 return response()->json(['name' => 'Abigail', 'state' => 'CA']);<br />

If you would like to create a JSONP response, you may use the json method in addition to<br />

setCallback:<br />

1 return response()<br />

2 ->json(['name' => 'Abigail', 'state' => 'CA'])<br />

3 ->setCallback($request->input('callback'));<br />

File Downloads<br />

The download method may be used to generate a response that forces the user’s browser to download<br />

the file at the given path. The download method accepts a file name as the second argument to the<br />

method, which will determine the file name that is seen by the user downloading the file. Finally,<br />

you may pass an array of HTTP headers as the third argument to the method:<br />

1 return response()->download($pathToFile);<br />

2<br />

3 return response()->download($pathToFile, $name, $headers);<br />

Note: Symfony HttpFoundation, which manages file downloads, requires the file being<br />

downloaded to have an ASCII file name.

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

Saved successfully!

Ooh no, something went wrong!