Laravel-如果用户从特定路径传递数据,则仅针对控制器中的特定功能禁用中间件

问题描述 投票:0回答:1

需求

如果数据是通过特定的路由传递到函数中的,那么中间件身份验证将仅针对特定的函数进行转义。

我尝试过的

所以我知道我可以使用以下方法来逃避中间件身份验证所需的特定功能但是,当我这样做时,它将忽略所有路由对spec_add函数的身份验证。

  public function __construct ()
    {
        $this -> middleware ( 'auth'  ,['except' => ['spec_add']]) ;
    }

我知道我可以实现以下类似的功能。但是我想知道的是,有一种比下面的方法更好的方法。

public function __construct ()
{
    if (  url () -> previous () == 'special_url'){
        $this -> middleware ( MiddlewareConstants::Auth . ':' . GuardConstants::Customers  ,['except' => ['addAddress']]) ;
    }else{
        $this -> middleware ( MiddlewareConstants::Auth . ':' . GuardConstants::Customers  ) ;

    }
}

谢谢

php laravel authentication url laravel-middleware
1个回答
1
投票

在您的web.php中试一试

Route::group(['middleware' => 'auth',], function () {
   // all the routes here will have to go through auth
});

// this will not
Route::get('spec_add', 'TestController@function');

希望有帮助。干杯!

© www.soinside.com 2019 - 2024. All rights reserved.