Laravel 5 checks if a user is logged in

I am new to Laravel 5 and trying to understand its process Auth. I want the user to not get to some of my pages if the user is not logged in. Trying to do this with help Route:filter, but that will not work. What have I done wrong?

Route::filter('/pages/mainpage', function()
{
    if(!Auth::check()) 
    {
        return Redirect::action('PagesController@index');
    }
});
+6
source share
5 answers

You must use authmiddleware . On your route, just add it like this:

Route::get('pages/mainpage', ['middleware' => 'auth', 'uses' => 'FooController@index']);

Or in the controller constructor:

public function __construct(){
    $this->middleware('auth');
}
+13
source

that way you can do it right in your blade code

@if (!Auth::guest())
        do this 
@else
        do that
@endif

+4
source

,

// Single route

Route::get("/awesome/sauce", "AwesomeController@sauce", ['middleware' => 'auth']);

, :

// Route group

Route::group(['middleware' => 'auth'], function() {
// lots of routes that require auth middleware
});
+2

middleware

public function __construct()
{
    $this->middleware('auth');
}
public function create()
{
    if (Auth::user()) {   // Check is user logged in
        $example= "example";
        return View('novosti.create')->with('example', $example);
    } else {
        return "You can't access here!";
    }
}
Route::get('example/index', ['middleware' => 'auth', 'uses' => 'example@index']);
+2

All Articles