如何禁止用户进入房屋?

时间:2019-07-29 13:02:17

标签: laravel

我在用户表中的列名为(active),如果(0)禁止他进入家并且如果(1)可以输入,则默认情况下该值为0,当然,在管理面板控件中使用该< / p>

所以我想说的是,在注册或登录后禁止他仍然通过admin ..p激活帐户。

我会为此创建中间件,但是不起作用!

public function handle($request, Closure $next)
 {
    if(auth()->user()->active == 1){
        return $next($request);
        return redirect('home');
    }else{
        return redirect('login');
    }
 }

和路线代码:

Auth::routes();
Route::group(['middleware' => 'is_active'], function() {

    Route::get('/home', 'HomeController@index')->name('home');

});

1 个答案:

答案 0 :(得分:2)

您是否在App\Http\Kernel.php类中注册了中间件?

$routeMiddleware数组中添加以下内容:

'is_active'       => 'App\Http\Middleware\YourMiddleware'

然后可以将用法简化为:


// check if the user is authenticated first because auth()->user() might return null which will throw an exception.
// also 1 is true anyway so no need to compare it. 0 is false.
if(auth()->check() && auth()->user()->active){
   return $next($request);
} else {

   // have to logout the user in order for the login page to show
   Auth::logout();

   return redirect('login')->with('message', 'Your account is being processed, please wait for approval');
}

login.blade.php中将此添加到某处:

@if(sessions()->has('message'))
<p>
    {{ session('message') }}
</p>
@endif