-
Attaching Headers
-
Attaching Cookies
-
View Responses
-
JSON Responses
-
File Downloads
-
File Responses
Generally, the routes and controllers should return a response to be sent back to the user's browser.
Laravel provides several different ways to return responses.
Basic Response, String and Array
The basic response that can be sent is simply returning a string from a route or controller. The framework will automatically convert the string into a HTTP response:
Route::get('/', function(){
return 'Hello Me';
});
- Output:
Hello Me
You may also return arrays. The framework will automatically convert the array into a JSON response:
Route::get('/', function(){
return ['happy'=>'love', 'give'=>'freedom', 'have'=>'peace', 8];
});
- Output:
{"happy":"love","give":"freedom","have":"peace","0":8}
Attaching Headers
HTTP headers can be attached to response. You may use the
header() method to add a series of headers to the response.
return response($content)
->header('Content-Type', $type)
->header('X-Header-One', 'Header Value')
->header('X-Header-Two', 'Header Value');
Or, you may use the
withHeaders() method to specify an array of headers to be added to the response:
return response($content)
->withHeaders([
'Content-Type'=> $type,
'X-Header-One'=> 'Header Value',
'X-Header-Two'=> 'Header Value',
]);
Attaching Cookies To Responses
The
cookie() method on response instances allows you to generate a cookie and attach it to the response.
return response($content)
->header('Content-Type', 'text/html')
->cookie('name', 'value', 24*7*60*60); //cookie expires after 7 days
- By default, all cookies generated by Laravel are encrypted and signed so that they can't be modified or read by the client.
View Responses
You can use the
view() helper function to return the content of a php file located in "
resources/views/" directory, or its sub-folders.
1. The following code looks for a view in
resources/views/home.blade.php or
resources/views/home.php , and loads its contents and parses any inline PHP.
Route::get('/', function(){
return view('home');
});
2. The following code loads the
resources/views/tasks/index.blade.php or
resources/views/tasks/index.php and passes it a single variable named
tasks , which contains the result of the
Task::all() method.
Route::get('tasks', function(){
return view('tasks.index')->with('tasks', Task::all());
});
3. You can also pass an array of variables to the view, as the second parameter:
Route::get('tasks', function(){
return view('tasks.index', ['k1'=>'value1', 'k2'=>'value-2']);
});
4. If you want to output the content of a view file, with custom HTTP
status and
header, you can use the
response()->view() method like in this example:
Route::get('/page',function(){
return response()->view('view_file', ['name'=>'value'], 200)->header('Content-Type', 'text/html');
});
JSON Responses
JSON response can be sent using the
json() method. This method will automatically set the
Content-Type header to
application/json, and convert the given array to JSON using the
json_encode() PHP function.
Route::get('json',function(){
return response()->json(['name'=>'Mar Plo', 'state'=>'Heaven']);
});
- Output:
{"name":"Mar Plo","state":"Heaven"}
File Downloads
The
download() method may be used to generate a response that forces the user's browser to download the file at the given path.
The download() method accepts a file name as the second argument, which will determine the file name that is seen by the user.
You may pass an array of HTTP headers as the third argument:
return response()->download($pathToFile);
return response()->download($pathToFile, $name, $headers);
return response()->download($pathToFile)->deleteFileAfterSend(true);
File Responses
The
file() method may be used to display a file, such as an image or PDF, directly in the user's browser instead of initiating a download.
This method accepts the path to the file as its first argument, and optionally, an array of headers as its second argument:
return response()->file($pathToFile);
return response()->file($pathToFile, $headers);
- Documentation:
Laravel - HTTP Responses