Laravel - Response

The web application responds to the user's request in many ways, depending on many parameters. This chapter explains in detail about responses in Laravel web applications.
Main Answer
Laravel provides several different ways to return a response. The response can be sent either from the route or from the controller. The main response that can be sent is a simple string, as shown in the following code example. This string will be automatically converted into the appropriate HTTP response.
example
Step 1 − Add the following code to app/Http/rout.php file .
app/http/routes.php
Route::get('/basic_response', function () { return 'Hello World'; });
Step 2 − Visit the following URL to check the main answer.
http://localhost:8000/basic_response
Step 3 − The output will look as shown in the following image.
Attaching headers
The response can be attached to headers using the header() method. We can also attach a series of titles, as shown in the code example below.
return response ( $content , $status ) -> header ( 'Content-Type' , $type ) -> header ( 'X-Header-One' , 'Header Value' ) -> header ( 'X-Header-Two' , 'HeaderValue' );
example
Take a look at the following example to understand more about Response −
Step 1 − Add the following code to app/Http/rout.php file .
app/http/routes.php
Route :: get ( '/header' , function () { return response ( "Hello" , 200 )-> header ( 'Content-Type' , 'text/html' ); });
Step 2 − Visit the following URL to check the main answer.
http://localhost:8000/header
Step 3 − The output will look as shown in the following image.
Attaching cookies
The withcookie() helper method is used to attach cookies. Cookies generated with this method can be attached by calling the withcookie() method with a response instance. By default, all cookies generated by Laravel are encrypted and signed, so they cannot be modified or read by the client.
example
Take a look at the following example to understand more about attaching cookies.
Step 1 − Add the following code to app/Http/rout.php file .
app/http/routes.php
Route :: get ( '/cookie' , function () { return response ( "Hello" , 200 )-> header ( 'Content-Type' , 'text/html' ) -> withcookie ( 'name' , 'Virat Gandhi ' ); });
Step 2 − Visit the following URL to check the main answer.
http://localhost:8000/cookies
Step 3 − The output will look as shown in the following image.
JSON Response
A JSON response can be sent using the json. This method automatically sets the Content-Type header to application/json . The json method will automatically convert the array into the corresponding json response .
example
Take a look at the following example to understand more about JSON Response −
Step 1 − Add the following line to app/Http/rout.php file .
app/http/routes.php
Route :: get ( 'json' , function () { return response ()-> json ([ 'name' => 'Virat Gandhi' , 'state' => 'Gujarat' ]); });
Step 2 − Visit the following URL to check the json response.
http://localhost:8000/json
Step 3 − The output will look as shown in the following image.