尝试在具有身份验证中间件的情况下公开路由

时间:2020-03-23 16:12:04

标签: php laravel laravel-5

你好,当我受到来自Middlewe的限制时,我无法访问几条路线

我的路线看起来像这样

Route::group([
    'middleware' => ['api','auth:sanctum'],
], function ($router) {
    Route::prefix('users')->group(function ($usersRoute) {
        Route::post('login', 'Api\Users\UserController@login');
        Route::post('logout', 'Api\Users\UserController@logout');
        Route::post('refresh', 'Api\Users\UserController@refresh');
        Route::post('profile', 'Api\Users\UserController@profile');
    });
    Route::prefix('leads')->group(function ($leadRoute) {
//        Route::get('look' , 'Api\Leads\LeadController@index');
//        Route::get('look/{id}' , 'Api\Leads\LeadController@find');
//        Route::post('look' , 'Api\Leads\LeadController@create');
//        Route::put('look/{id}' , 'Api\Leads\LeadController@update');
//        Route::delete('look/{id}' , 'Api\Leads\LeadController@archived');
//        Route::put('look/{id}/view' , 'Api\Leads\LeadController@toggleView');
        Route::prefix('assets')->group(function ($leadRoute) {
            Route::get('/' , 'Api\Leads\AssetController@index');
            Route::post('/' , 'Api\Leads\AssetController@create');
            Route::get('/show/random' , 'Api\Leads\AssetController@showRandomAsset');
            Route::delete('/{id}' , 'Api\Leads\AssetController@archived');
            Route::delete('/autoclear' , 'Api\Leads\AssetController@autoClear');
            Route::put('/{id}/show' , 'Api\Leads\AssetController@toggleShow');
        });
    });
});

现在我有两个控制器,承包商看起来像这样

class UserController extends BaseAPIController
{
    /**
     * Create a new AuthController instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:sanctum', ['except' => ['login']]);
    }

当我查看路线列表时

api,auth:sanctum                                     |
|        | POST     | api/users/login                        |                       | App\Http\Controllers\Api\Users\UserController@login            | api,auth:sanctum                                     |
|        | POST     | api/users/logout                       |                       | App\Http\Controllers\Api\Users\UserController@logout           | api,auth:sanctum                                     |
|        | POST     | api/users/profile                      |                       | App\Http\Controllers\Api\Users\UserController@profile          | api,auth:sanctum                                     |
|        | POST     | api/users/refresh                      |                       | App\Http\Controllers\Api\Users\UserController@refresh          | api,auth:sanctum                

我尝试的是对所有人免费的免费登录路由,但我得到了

{ “ message”:“未经身份验证”。 } 我有点迷路了,请帮助

2 个答案:

答案 0 :(得分:0)

当我查看laravel的文档时,发现:

$ this-> middleware('subscribed')-> except('store');

函数中间件的第二个参数用于选项,那么尝试一下:

$ this-> middleware('auth:sanctum')-> except('login');

答案 1 :(得分:0)

在您的代码中,您两次包含了中间件,这是不对的。

您应该在路由文件中删除组中间件,因为您已经在构造函数方法中注入了auth中间件。

删除

  Route::group([
    'middleware' => ['api','auth:sanctum'],
], function ($router) { 

此后,您的代码将按预期工作,因为每次实例化auth:sanctum中间件时,它将仅在您的控制器中调用一次,并且将排除登录路径。