获取验证CsrfToken 尝试在添加中间件时获取非对象的属性



我正在添加一个中间件,用于检查数据库中的特定权限

中间件

public function handle($request, Closure $next)
{
$right = User_access_right::where('access_key','=','add')
->where('user_id','=',Auth::user()->id)
->first();
if(count($right) == 1)
{
if($right->access_value == 1)
{
return $next($request);
}
}
else
{
return redirect()->to('/home')->withErrors(['status' => 'You do not have privilege to do such task']);
}
}

现在在控制器中,我已经添加到了一些方法中。

public function __construct()
{
$this->middleware('auth');
$this->middleware('hasAddPrivilege', ['only'=>['CreateCategory', 'CreateSubCategory']]);
}

在VerifyCsrfToken中.php我添加了一些路由来不检查令牌

protected $except = [
'/category/create',
'/category/update',
'/category/sub_create',
'/category/move_category',
'/item/create',
'/item/update'
];

和我的路线

Route::group(['middleware' => 'auth', 'prefix' => '/category'],     function()
{
Route::get('/', 'CategoryController@Category');
Route::post('/create', 'CategoryController@CreateCategory');
Route::post('/update', 'CategoryController@UpdateCategory');
Route::post('/sub_create', 'CategoryController@CreateSubCategory');
Route::get('/other_category/{id}', 'CategoryController@getRestOfCategories');
Route::post('/move_category', 'CategoryController@MoveCategory');
Route::get('/getCategoryTree', 'CategoryController@getCategoryTree');
});

但是我仍然收到此错误;

VerifyCsrfToken 中的 ErrorException.php第 156 行:尝试获取非对象的属性

您的中间件有一个逻辑分支,根本不返回任何内容。中间件需要返回响应,以便以前的中间件可以在需要时对响应执行操作。他们接受请求并返回响应。

VerifyCsrfToken得到一个null作为中间件的响应,因为根本没有返回任何内容。

也有可能这个中间件之后的下一个中间件没有正确返回响应,并且正在返回,这将导致相同的问题。

if(count($right) == 1)
{
if($right->access_value == 1)
{
return $next($request);
}
// what about when $right->access_value != 1 ?
}

最新更新