Laravel 9 Response Example Tutorial

May 10, 2022 . Admin

Hi All,

Today, in this article, I will share something unique content for you how to response actually works in laravel 9.you can easily understand all response learn in one tutorial. All Laravel routes and controllers must return a response that can be sent back to the user’s browser.

Here I will give you all of the response how it works in one example tutorial so you can just follow step by step.

Download Laravel

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

composer create-project laravel/laravel example-app
Responses Strings

In this first example I will show you the 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 'MyWebtuts.com';
}
Responses Arrays

In this example returning a array from a route or controller.

<?php

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return ['Ramesh','Suresh','Mahesh'];
}
Responses Objects

In this response returning string from a route or controller with status code 200 and passes to the header.

<?php

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return response('MyWebtuts.com', 200)
              ->header('Content-Type', 'text/plain');
}
Responses Redirects

In this controller method, the response redirect to the home dashboard route.you can use one URL to redirect another URL user.

<?php

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return redirect('home');
}
Responses Back

back() response use to user back to after URl.you can multiform write than any query before form at a 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([FormController::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

Responses with use specific URL with display message in your browser.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

Today,Most of the APIs 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 utilized in the json_encode PHP function.

<?php

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return response()->json([
        'name' => 'Bhavesh',
        'state' => 'Gujarat',
    ]);
}
Responses Download

This response is use to pdf file download in your own desktop.

<?php

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    return response()->download($pathToFile);
}
Responses File

This response is to use image and file download in your own 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