我想将 /account/{account_id}
分组以及使用 auth
中间件和中间件来保护其中的每个路由,该中间件将检查记录的用户是否可以访问此帐户 . 不幸的是它不起作用 .
这是我的代码
web.php:
Route::group(['middleware' => 'auth'], function () {
Route::group(['prefix' => 'account/{account}', 'middleware' => 'userHasPermissionForAccount'], function() {
Route::group(['prefix' => 'posts'], function () {
Route::get('{post}', 'PostsController@index')->where([
'post' => '\d+'
]);
});
// more routes here...
});
});
app / http / kernel.php
// ...
protected $routeMiddleware = [
// ...
'userHasPermissionForAccount' => \App\Http\Middleware\UserCanAccessContent::class,
];
它甚至没有触发我的自定义中间件中的代码,我不明白为什么 .
1 回答
我会使用策略(门),因为您可以使用策略作为中间件 .
在 AuthServiceProvider 注册该政策:
在内部策略文件( app/policies/AccountPolicy.php )中,create方法可以说"manage"
然后将此策略用作中间件: