Muhammad
Muhammad

Reputation: 3250

What is the difference between return or echo the view? return view is talking longer then echo view

what is the best practice to load a view.

documentation is saying load view here https://laravel.com/docs/5.7/views

following is my code:

 if (isset($results['status'])) {
           $error [] = $results['msg'];
           $request->session()->flash('message.level', 'danger');
           $request->session()->flash('message.content', $error);
 } else {
           if (view()->exists('import.device.results')) {
                  echo view('import.device.results', compact('results'));
                  exit;
           }
 }

when i use

echo view('import.device.results', compact('results'));

it takes a second and when i use

return view('import.device.results', compact('results'));

it takes like 10 seconds

my view file:

@extends('layouts_blue.master')
@section('content')
    <!--content-->

    <script language="JavaScript">
    </script>
    <div class="container content-body table-responsive" id="no-more-tables1">

        @if(session()->has('message.level'))
            <div class="alert alert-{{ session('message.level') }} import-device-error">
                <ul class="fa-ul">
                    @foreach (session('message.content') as $error)
                        <li>{!! $error !!}</li>
                    @endforeach
                </ul>
            </div>
        @endif

    </div>
    </div>
@stop

Upvotes: 3

Views: 925

Answers (2)

Jignesh Joisar
Jignesh Joisar

Reputation: 15135

return If called from within a function, the return statement immediately ends execution of the current function, and returns its the value of the function called. passing a value to another function or variable.

echo outputs a value one or more

echo prints the value so you can read it.

return returns the value to save in variable.

In laravel same you can store view in variable by return.

In middleware return used to stop a execution or pass to the next function

In return u can return as you type like Json or object or many array with value.

Upvotes: 2

Devon Bessemer
Devon Bessemer

Reputation: 35337

Laravel performs a lot of necessary actions after it gets the response from your controller. By exiting from your controller, you are terminating the request in the middle of its lifecycle:

  • Terminable middleware will not be run. Middleware in most frameworks can run before the request is sent to the controller and after the response is received from the controller.
  • Terminating callbacks (registered in the application/container) will not be executed.
  • Session may not be persisted. Depending on the driver being used, session data may not be persisted until after the controller returns a response.
  • All of your cookies and headers may not be sent.
  • The response will not be automatically converted to a string or JSON.

Upvotes: 5

Related Questions