在控制器中的构造方法中创建的中间件抛出错误

时间:2018-01-24 02:07:29

标签: php laravel web laravel-5 laravel-5.5

我试图将用户限制在我的控制器中的某些方法,因此我不得不在我的控制器中的构造方法中创建一个中间件,它首先工作,然后当我编写第二个中间件时,所有地狱都崩溃了。

 public function __construct(){
    /*
        @Thirdwrist
        This is a a middleware that checks if a user has registered
        a profile, if the user is registered it redirects to the index
        of the profile controller, else it allows the user to access the 
        methods.
    */ 
    $this->middleware(function($request, $next){

        $id = Auth::id();
        if(Profile::where('user_id',$id)->count()){

            return redirect()->Route('profile.index');
        }
        else{
            return $next;
        }
    })->only(['create', 'store']);


    /*
        @Thirdwrist
        Middleware which checks if the user has a profile
    */ 
    $this->middleware(function ($request, $next){
        if (!Profile::where('user_id',Auth::id())->count()) {

            return redirect()->Route('profile.create');
        }
    })->except(['create', 'store']);
}

错误

  

Symfony \ Component \ Debug \ Exception \ FatalThrowableError(E_ERROR)   Closure对象不能具有属性

它指向laravel中的代码

 {
    $config = config('session');

    $response->headers->setCookie(
        new Cookie(
            'XSRF-TOKEN', $request->session()->token(), $this->availableAt(60 * $config['lifetime']),
            $config['path'], $config['domain'], $config['secure'], false, false, $config['same_site'] ?? null
        )
    );

    return $response;
}

}

$ response-> headers-> setCookie

的行

1 个答案:

答案 0 :(得分:1)

您需要将$request传递给$next关闭。

$this->middleware(function ($request, $next) {
    $id = Auth::id();

    if (Profile::where('user_id', $id)->count()) {
        return redirect()->Route('profile.index');
    }

    return $next($request);
})->only(['create', 'store']);