Laravel 9 Response Tutorial

10-Apr-2023

.

Admin

Laravel 9 Response Tutorial

Hi Guys,

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

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

Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip the following step.

composer create-project laravel/laravel example-app

Add Controller

php artisan make:controller HomeController

Responses Strings

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

<?php

/**

* 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.

<?php

/**

* 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.

<?php

/**

* 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.

<?php

/**

* 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.

<?php

/**

* 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.

<?php

/**

* 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.

<?php

/**

* 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.

<?php

/**

* 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.

<?php

/**

* 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 the json_encode PHP function.

<?php

/**

* 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.

<?php

/**

* 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.

<?php

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

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

}

It will help you...

#Laravel 9