drrqwokuz71031449 2017-05-30 15:46
浏览 33
已采纳

Laravel 5.4中的其他auth中间件

I want to add additional middleware to user model. User table has row 'approved' which is boolean (default is false). So when user logins - middleware should check if 'approved' is equal to true. If not, redirect to error page. What i got so far:

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class ConfirmedMiddleware
{
    public function handle($request, Closure $next, $guard = null)
    {
        if(Auth::user()->approved != true){
            redirect('/error');
        }
        return $next($request);
    }
}

So far middleware attached here:

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

However, it does not work. No errors as well.

  • 写回答

2条回答 默认 最新

  • duan0821 2017-05-30 15:54
    关注

    You're better off checking this when a user logins instead of using a middleware.

    Add this to the LoginController and perform any additional checks. This method will be called after the user successfully logs in.

    protected function authenticated($request, $user)
    {
        if($user->approved != true){
            return redirect('/error');
        }
    }
    

    If you still insist on using middleware, then make sure you add the middleware in Kernel.php

    Add this in app/Http/Kernel.php

    'confirmed' => \App\Http\Middleware\ConfirmedMiddleware::class,
    

    You're also missing the return in your redirect.

    public function handle($request, Closure $next, $guard = null)
    {
        if(Auth::user()->approved != true){
            return redirect('/error');
        }
    
        return $next($request);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 WPF动态创建页面内容
  • ¥15 如何对TBSS的结果进行统计学的分析已完成置换检验,如何在最终的TBSS输出结果提取除具体值及如何做进一步相关性分析
  • ¥15 SQL数据库操作问题
  • ¥100 关于lm339比较电路出现的问题
  • ¥15 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线