mmarques
mmarques

Reputation: 296

Laravel 5 Routing with Auth

Hello!

I'm trying to learning laravel, and i started with doing a page that only logged people can access ( I don't have login yet ) , but i'm just testing, so far i have:

Routes.php:

<?php

Route::get('/', ['as' => 'index', function () {
    return view('index');
}]);


Route::get('/home', function () {
    return view('home');
});

Ok, this file contains the routes , i searched how to give a name to the route, so i can redirect user to that route when he don't have access.

My other file is home.blade.php:

<?php 

if (!Auth::check()){    
    return Redirect::route('index');        
} 

?>

So is that the best way to check if user is logged? I gave a name to the route, index , so if user is not logged i want to redirect back to index, is not working tho, i want to know if this is the best option to achieve my goal.

Thank you.

Upvotes: 0

Views: 424

Answers (2)

ujwal dhakal
ujwal dhakal

Reputation: 2459

use Auth; loading facades

if (Auth::check()){    

if(Auth::Guest()):
return Redirect::route('index');       
endif; 

} 

?>

Upvotes: 0

skad0
skad0

Reputation: 163

The best way, as said at documentation is:

    Route::get('/home', ['middleware' => 'auth', function () {
        // here will be only authorized access
        return view('home');
    }]);

Auth middleware defined by default.

Upvotes: 1

Related Questions