EChan42
EChan42

Reputation: 139

Redirection from view or routesfile, Laravel

Which is better practice?

A- have my "home.blade.php" view show different content depending on if the user is logged in or not using (Auth::check())

or

B- Have my routes file redirect users to different views depending on them being logged in or not.

I'm guessing B but I'm not sure in how to implement two different views under the same route.

Now I have the following:

Route::get('', [
'uses'  => '\App\Http\Controllers\HomeController@index',
'as'    => 'home',

Upvotes: 0

Views: 409

Answers (2)

evandrobm
evandrobm

Reputation: 445

Two options are good, depending on what you have in this view. If you only had a "You are logged in"/"You're not logged in" message, make it in view using blade is cool.

If you will had two different pages, with lots of content, you view file will be to big and confused, so i believe that best practice will make two views and verify if user was logged in your controller.

The better practice here depends on your implementation

Upvotes: 1

Alex Meyer
Alex Meyer

Reputation: 235

you can use something like that in your blade template view file to show content unless the user logged in..

 @unless (Auth::check())
     You are not signed in.
 @endunless

or if an user is logged in with

 @if(Auth::check())
    You are signed in.
 @endif

if you want to serve a custom view to each user (guest, loggedin) then you can do it in the controller with

if(Auth::check()) {
    return view('user');
}

// otherwise
return view('guest');

Upvotes: 1

Related Questions