Laravel 5检查用户是否登录 [英] Laravel 5 check whether a user is logged in

查看:903
本文介绍了Laravel 5检查用户是否登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel 5的新手,正在尝试了解其Auth流程.我想阻止用户访问我的某些页面,除非该用户未登录.尝试使用Route:filter使其无法工作.我做错了什么?

I am new to Laravel 5 and trying to understand its Auth process. I want to prevent user to reach some of my pages unless the user is not logged in. Trying to make it with Route:filter but it does not work. What i have done wrong ?

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

推荐答案

您应使用 auth中间件.在您的路线中,像这样添加它:

You should use the auth middleware. In your route just add it like this:

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

或者在您的控制器构造函数中:

Or in your controllers constructor:

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

这篇关于Laravel 5检查用户是否登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆