Laravel Responses JSON with Status Code: In this tutorial, we are going to share how to use or return response with json or string etch in laravel 9 application.
In Laravel routes and controllers must return a response and we can be sent back to user’s browser. All the web application send responses according the user requests. If we are talk about laravel, Laravel provides several different ways to return responses.
Here we have mentioned all laravel reponsed, like, string, array, json, with redirect etc. You can use return response example tutorial in your laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 version as well. Lets see the seprate examples of all responses…
1. Return Strings in Response
The most basic response is returning a string from a route or controller. The framework will automatically convert the string into a full HTTP response:
public function index()
{
return 'codingdriver.com';
}
2. Return Array in Response
In addition to returning strings from your routes and controllers, you may also return arrays. The framework will automatically convert the array into a JSON response:
public function index()
{
return [1, 2, 3];
}
3. Return Object in Response
Returning a full Response
instance allows you to customize the response’s HTTP status code and headers. A Response
instance inherits from the Symfony\Component\HttpFoundation\Response
class, which provides a variety of methods for building HTTP responses:
public function index()
{
return response('codingdriver.com', 200)
->header('Content-Type', 'text/plain');
}
4. Return Redirect with Response
Redirect responses are instances of the Illuminate\Http\RedirectResponse
class, and contain the proper headers needed to redirect the user to another URL. There are several ways to generate a RedirectResponse
instance. The simplest method is to use the global redirect
helper:
public function index()
{
return redirect('dashboard')->with('success', 'Successfully worked.');
}
// success message in blade file
@if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
5. Response Redirects Back
The Back response use to user back to after URl.you can multi form write then any query before form at time back response use.
public function index()
{
return back()->withInput();
}
6. Response redirect Routes
When you call the redirect
helper with no parameters, an instance of Illuminate\Routing\Redirector
is returned, allowing you to call any method on the Redirector
instance.
public function index()
{
return redirect()->route('login');
}
7. Responses Redirect Domains
Sometimes you may need to redirect to a domain outside of your application. You may do so by calling the away
method, which creates a RedirectResponse
without any additional URL encoding, validation, or verification:
public function index()
{
return redirect()->away('https://www.twitter.com');
}
8. Responses Back
The Back response use to user back to after URl. Yyou can multi form write then any query before form at time back response use.
public function index()
{
return back()->withInput();
}
9. Responses Action
You may also generate redirects to controller actions. To do so, pass the controller and action name to the action method:
public function index()
{
return redirect()->action([UserController::class, 'index']);
}
10. Return json Responses
The json
method will automatically set the Content-Type
header to application/json
, as well as convert the given array to JSON using the json_encode
PHP function:
public function index()
{
return response()->json([
'name' => 'Test',
'city' => 'Delhi',
]);
}
11. 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 an array of headers as its second argument:
public function index()
{
return response()->file($pathToFile);
}
12. Responses File Download
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 filename as the second argument to the method, which will determine the filename that is seen by the user downloading the file. Finally, you may pass an array of HTTP headers as the third argument to the method:
public function index()
{
return response()->download($pathToFile);
// return response()->download($pathToFile, $name, $headers);
}
13. Attaching Headers To Responses
For example, you may use the header
method to add a series of headers to the response before sending it back to the user:
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',
]);
I hope you enjoy with laravel 8 responses tutorial..