我想对/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,
];它甚至不会触发我的自定义中间件中的代码,我不明白为什么。
发布于 2017-06-22 18:57:28
我将使用策略(门),因为您可以将策略用作中间件。
$ php artisan make:policy AcccountPolicy在AuthServiceProvider中注册策略
/**
 * The policy mappings for the application.
 *
 * @var array
 */
protected $policies = [
    Account::class => AccountPolicy::class, //Account is model, remember to import!
];在策略文件(app/policies/AccountPolicy.php),创建方法中,让我们说“管理”
/**
 * Determine if ....
 *
 * @param  \App\User  $current
 * @param  \App\Account  $account
 * @return bool
 */
public function manage(User $current, Account $account)
{
    //return some logic here to check if $current is part of $account
}然后使用此策略作为中间件:
Route::group(['prefix' => 'account/{account}', 'middleware' => 'can:manage,account'], function...https://stackoverflow.com/questions/44686936
复制相似问题