WEB DEVELOPER SITE
TUTORIALS HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Laravel - Response


Basic Response

Laravel gives a few several different approaches to return Response. Response can be sent either from route or from controller. The fundamental Response that can be sent is straightforward string as appeared in the underneath test code. This string will be consequently changed over to proper HTTP response.

Example

Step 1 − Add the accompanying code to app/Http/routes.php file.

app/Http/routes.php

Route::get('/basic_response', work () { 

return 'Hi World'; 

}); 

Step 2Visit the accompanying URL to test the essential response.

http://localhost:8000/basic_response 

Step 3 − The yield will show up as appeared in the accompanying image.

Basic Response

Attaching Headers

The Response can be appended to headers utilizing the header() method. We can likewise join the arrangement of headers as appeared in the beneath test code.

return response($content,$status) 

->header('Content-Type', $type) 

->header('X-Header-One', 'Header Value') 

->header('X-Header-Two', 'Header Value');

Example

Observe the accompanying guide to see understand about Response −

Step 1 − Add the accompanying code to app/Http/routes.php file.

app/Http/routes.php

Route::get('/header',function(){ 

return response("Hello", 200)- >header('Content-Type', 'text/html'); 

});

Step 2 − Visit the accompanying URL to test the essential response.

http://localhost:8000/header 

Step 3 − The yield will show up as appeared in the accompanying image.

Hello

Attaching Cookies

The withcookie() aide strategy is utilized to join cookies. The cookies created with this method can be joined by calling withcookie() strategy with Response occasion. As a matter of route , all cookies created by Laravel are encrypted and marked with the goal that they can't be changed or modified by the client.

Example

Observe the accompanying guide to see understand about joining cookies −

Step 1 − Add the accompanying code to app/Http/routes.php file.

app/Http/routes.php

Route::get('/cookie',function(){ 

return response("Hello", 200)- >header('Content-Type', 'text/html') 

->withcookie('name','Virat Gandhi'); 

});

Step 2Visit the accompanying URL to test the fundamental response.

http://localhost:8000/cookies 

Step 3 − The yield will show up as appeared in the accompanying image.

Hello

JSON Response

JSON Response can be sent utilizing the json method. This strategy will consequently set the Content-Type header to application/json. The json strategy will consequently change over the cluster into suitable json response.

Example

Observe the accompanying guide to see progressively about JSON Response −

Step 1 − Add the accompanying line in app/Http/routes.php file.

app/Http/routes.php

Route::get('json',function(){ 

return response()- >json(['name' => 'Virat Gandhi', 'state' => 'Gujarat']); 

});

Step 2 − Visit the accompanying URL to test the json response.

http://localhost:8000/json