我在构造函数中有一个包含以下内容的控制器:

$this->middleware('guest', ['except' =>
    [
        'logout',
        'auth/facebook',
        'auth/facebook/callback',
        'auth/facebook/unlink'
    ]
]);

The 'logout' rule (which is there by default) works perfectly but the other 3 rules I have added are ignored. The routes in routes.php look like this:

Route::group(['middleware' => ['web']],function(){

    Route::auth();

    // Facebook auth
    Route::get('/auth/facebook', 'Auth\AuthController@redirectToFacebook')->name('facebook_auth');
    Route::get('/auth/facebook/callback', 'Auth\AuthController@handleFacebookCallback')->name('facebook_callback');
    Route::get('/auth/facebook/unlink', 'Auth\AuthController@handleFacebookUnlink')->name('facebook_unlink');
}

If I visit auth/facebook, auth/facebook/callback or auth/facebook/unlink whilst logged in I get denied by the middleware and thrown back to the homepage.

我已经try 使用/指定"例外"规则,以便它们与routes.php中的路由完全匹配,但没有什么不同.你知道为什么这些规则被忽略了,而默认的"注销"规则却被尊重了吗?

干杯!

推荐答案

You need to pass the method's name instead of the URI.

<?php
    
namespace App\Http\Controllers;
    
class MyController extends Controller {
    public function __construct() {
        $this->middleware('guest', ['except' => [
            'redirectToFacebook', 'handleFacebookCallback', 'handleFacebookUnlink'
        ]]);
    }
}

自Laravel 5.3以来,您可以使用fluent界面在控制器上定义中间件,这似乎比使用多维数组更简洁.

<?php

$this->middleware('guest')->except('redirectToFacebook', 'handleFacebookCallback', 'handleFacebookUnlink');

Laravel相关问答推荐

如何通过单击进入 vs 代码中的类

验证错误后的 Laravel 自定义重定向

给定的角色或权限应该使用 guard `` 而不是 `web`. -Laravel

如何在 Laravel 中使用内存数据库的完整测试套件之前迁移和 seeder ?

Laravel Blade:@stop VS @show VS @endsection VS @append

Laravel 控制器中一种特定方法的中间件

Laravel 5.7 判断Electron邮件是否经过验证

使用 Laravel 限制多态多对多关系中的相关记录

如何从 Laravel 中的 hasMany() 关系中获取所有结果?

计算laravel中查询返回的行数

Laravel 随机排序

将自定义消息(或任何其他数据)传递给 Laravel 404.blade.php

如何在laravel 5.1中使用url(路由)传递多个参数

调用未定义的方法 Illuminate\Routing\Route::get()

Laravel - DecryptException:'MAC 无效'

我在哪里放置可以显示 Flash 消息的 Laravel 4 辅助函数?

Laravel hasManyThrough

在 Laravel 5.4 中将文件存储在公共目录和存储中的区别

使用 Queue::fake() 测试监听器

laravel中的动态网址?