Laravel response



What is Response in Laravel?

  • Reponse in Laravel is HTTP Response object you are sending back to your visitor's browser, after the visitor(client browser) send a Request to one of your Routes.
  laravel response
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts

Basic Response:

  • Each request has a response.
  • Laravel provides several different ways to return response.
  • Response can be sent either from route or from controller.
  • The basic response that can be sent is simple string as shown in the below sample code.
  • This string will be automatically converted to appropriate HTTP response.

Example:

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

app/Http/routes.php

Route::get('/basic_response', function () {
   return 'wikitechy';
});
  • Step 2 − Visit the following URL to test the basic response.
  • http://localhost:8000/basic_response
  • step 3 − The output will appear as shown in the following image.
 laravel redirect with message
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts

Attaching Headers:

  • The response can be attached to headers using the header() method.
  • We can also attach the series of headers as shown in the below sample code.
return response($content,$status)
   ->header('Content-Type', $type)
   ->header('X-Header-One', 'Header Value')
   ->header('X-Header-Two', 'Header Value');

Example:

  • step 1 − Add the following code to app/Http/routes.php file.

app/Http/routes.php

Route::get('/header',function(){
   return response("wikitechy", 200)->header('Content-Type', 'text/html');
});
  • step 2 − Visit the following URL to test the basic response.
  • http://localhost:8000/header
  • step 3 − The output will appear as shown in the following image.
 laravel redirect with message

Attaching Cookies:

  • The withcookie() helper method is used to attach cookies.
  • The cookie generated with this method can be attached by calling withcookie() method with response instance.
  • By default, all cookies generated by Laravel are encrypted and signed so that they can't be modified or read by the client.
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts

Example:

  • step 1 − Add the following code to app/Http/routes.php file.

app/Http/routes.php

Route::get('/cookie',function(){
   return response("wikitechy", 200)->header('Content-Type', 'text/html')
      ->withcookie('name','Virat Gandhi');
});
  • step 2 − Visit the following URL to test the basic response.
  • http://localhost:8000/cookie
  • step 3 − The output will appear as shown in the following image.
 laravel redirect with message

JSON Response:

  • JSON response can be sent using the json method.
  • This method will automatically set the Content-Type header to application/json.
  • The json method will automatically convert the array into appropriate json response.
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts

Example:

  • step 1 − Add the following line in app/Http/routes.php file.

app/Http/routes.php

Route::get('json',function(){
   return response()->json(['name' => 'sachin', 'state' => 'mumbai']);
});
  • step 2 − Visit the following URL to test the json response.
  • http://localhost:8000/json
  • step 3 − The output will appear as shown in the following image.
 laravel redirect with parameters


This tutorial is used to learn laravel and also provides guidance on laravel book , laravel books , laravel hosting , laravel programming , laravel server php , laravel server , laravel development server , start laravel server , laravel getting started , laravel programmer , php with laravel for beginners , what is laravel framework in php , up and running with laravel , php framework laravel , getting started with laravel , what is laravel framework , laravel applications , what is laravel in php , laravel hosting , laravel framework , php laravel framework , latest framework in php

Related Searches to Laravel - response