Laravel 5 检查用户是否登录 [英] Laravel 5 check whether a user is logged in
本文介绍了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屋!
查看全文