Laravel HTTP Response
Laravel HTTP Response – The Laravel HTTP Response is used to return a response. The response can be sent either from route or controller. All request has a response. The string will be automatically converted into the Http response.
Laravel HTTP Response.
Let us understand how to use laravel Http Response.
Function:-
There are some followings function available in laravel Http Response.
- 1. Creating Response.
- 2. Redirects.
- 3. Other Response Types.
- 4. Response Macros.
1. Creating Response.
Strings & Arrays
All route and controller should return a response to be sent back to the user browser. It will automatically convert string to Http response.
Route::get('response/', function () { return 'Hello World'; });
You can also return string from route or controller to array. laravel will automatically convert the array into a json response.
Route::get('response1/', function () { return [1, 2, 3]; });
Response Objects
You want be returning simple string or array from the route action. You will be returning full Illuminate/Http/Response instances or views.
Route::get('home', function () { return response('Tutorialsplane.com', 200) ->header('Content-Type', 'text/plain'); });
Attaching Headers To Responses
We can use the header method to add a series of header to the response before sending it back to the user.
Route::get('AttachHeader', function () { $content = 'Soild Coupon!'; $type = 'text/plain'; return response($content) ->header('Content-Type', $type) ->header('X-Header-One', 'Header Value') ->header('X-Header-Two', 'Header Value'); });
Attaching Cookies To Responses
This method allows you to easily attach cookie to the response. you can use the cookie
method to generate cookie.
Route::get('cookie', function () { $content = 'Tutorialsplane.com!'; $type = 'text/plain'; $minutes = 1; return response($content) ->header('Content-Type', $type) ->cookie('name', 'value', $minutes); });
Cookies & Encryption
Laravel are generated all cookie which is encrypted and signed so they can’t be modify or read by the client. If you want to disable encryption you may use $except property.
protected $except = [ 'cookie_name', ];
2. Redirects.
Redirect response are motive of the Illuminate/Http/RedirectResponse class. It is used to redirect the user to another url.
Route::get('home', function () { return redirect('home/dashboard'); });
Sometime user want to redirect to their previous location during the form validation. That time you may use back helper function.
Route::post('user/profile', function () { return back()->withInput(); });
Redirecting To named Routes
To use redirect helper with no parameter, you can use route method.
return redirect()->route('create');
You can also pass the parameter as a second argument in route method.
]
return redirect()->route('create', ['id' => 1]);
Redirecting To Controller Actions
You can also redirect to controller action. It’s a easy to do simply pass the controller and action name to the action method. You don’t need to specify full namespace to controller. it will automatically set the base controller.
return redirect()->action('HomeController@index');
if your controller route required patameter. Then you can pass second argument to the action method.
return redirect()->action( 'UserController@profile', ['id' => 1] );
Redirecting With Flashed Session Data
Redirecting to a new URL and flash data to the session are done. This is done after successful performing action when you give a success or failure message to the session.
Route::post('profile', function () { return redirect('dashboard')->with('status', 'Profile updated!'); });
After the user is redirected, you may display the flashed message on the view page like this:-
@if (session('status')) <div class="alert alert-success"> {{ session('status') }} </div> @endif
3. Other Response Types.
The response helper is used to generate other type of response. When We called this helper without argument, contract is returned which is provide some helpful method for generating response.
View Responses
We use View method when we need to control over response status and header but also need to return on view.
return response() ->view('hello', $data, 200) ->header('Content-Type', $type);
JSON Responses
The json method will automatically set the content-type and header to application/json and convert to given array to json.
Route::get('json',function(){ return response()->json(['name' => 'Sonu Kumar', 'state' => 'Bihar']); });
File Downloads
The download method may be used generate a response that forcly download the file at the given path. This method accept the second parameter as a file name and third is Http header.
return response()->download($pathToFile); return response()->download($pathToFile, $name, $headers); return response()->download($pathToFile)->deleteFileAfterSend(true);
File Responses
The file method is used to diaplay the file in the form of image or PDF. This method accept first parameter is file path and second is array of header.
return response()->file($pathToFile); return response()->file($pathToFile, $headers);
3. Response Macros.
You want to define a custom response that you can re-use variety of your route and controller.
You can use macro method on the response facade.
Let’s look at a simple example.
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Response; class ResponseMacroServiceProvider extends ServiceProvider { public function boot() { Response::macro('caps', function ($value) { return Response::make(strtoupper($value)); }); } } |
The macro function accept a name as its first argument and a closure as its second.
return response()->caps('foo');
Advertisements