Laravel 8 Response Tutorial

Laravel 6 , Laravel , Laravel 7 , Laravel 8

Nicesnippets

197

30-09-2020


Hi Guys,

In this tutorial,I will learn you how to response use in laravel 8.you can easy all resopnse learn in tutorial.All Laravel routes and controllers must return a response which can be sent back to user’s browser.

A web application responds to a user’s request in many ways depending on many parameters. This chapter explains you in detail about responses in Laravel web applications.

Responses Strings


In this response,The most basic response is returning a string from a route or controller.

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

return 'nicesnippets.com';

}

Responses Arrays

In this response,The most basic response is returning a array from a route or controller.

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

return [1, 2, 3];

}

Responses Objects

In this response,The basic and simply response is returning string from a route or controller.

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

return response('nicesnippets.com', 200)

->header('Content-Type', 'text/plain');

}

Responses Redirects

In this method,the response redirect is use login user redirect to dashboard.you can use one URl to rediect another URL user.

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

return redirect('home/dashboard');

}

Responses Back

The Back response use to user back to after URl.you can multi form write then any query before form at time back response use.

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

return back()->withInput();

}

Responses Routes

this response is use to specifice route redirect this URL.

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

return redirect()->route('login');

}

Responses Action

this response is use redirect other controller specifice method call.action call controller in use in your controller.

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

return redirect()->action([TestController::class, 'index']);

}

Responses Domains

this response is use specifice domain redirect.

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

return redirect()->away('https://www.facebook.com');

}

Responses With

this response is use specifice URl with display message in your bowser.this is done after successfully performing an action when you flash a success message to the session.

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

return redirect('dashboard')->with('status', 'Profile updated!');

}

//display error blade message

@if (session('status'))

<div class="alert alert-success">

{{ session('status') }}

</div>

@endif

Responses Json

Most of the APIs today reflect JSON data. One of the reasons why Laravel is so famous is that it has JSON support right out of the box. That is we don’t need to make any programmatic efforts for this. Json method of Laravel automatically converts the array into JSON form which is used in json_encode PHP function.

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

return response()->json([

'name' => 'Ravi',

'state' => 'Guj',

]);

}

Responses Download

this response is use to pdf file download in your desktop.

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

return response()->download($pathToFile);

}

Responses File

this response is use image and file download in your desktop.

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

return response()->file($pathToFile);

}

It will help you..


Recommended Posts