WEB DEVELOPER SITE
TUTORIALS HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Laravel - Response


Basic Response

Laravel gives a few distinctive approaches to return reaction. Reaction can be sent either from course or from controller. The fundamental reaction 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 reaction can be appended to headers utilizing the header() technique. 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 increasingly 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 treats. The treat created with this technique can be joined by calling withcookie() strategy with reaction occasion. As a matter of course, all treats created by Laravel are scrambled and marked with the goal that they can't be changed or perused by the client.

Example

Observe the accompanying guide to see increasingly about joining treats −

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/treat 

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

Hello

JSON Response

JSON reaction can be sent utilizing the json technique. 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