【问题标题】:Removing a route from Auth::routes()从 Auth::routes() 中删除路由
【发布时间】:2017-05-12 16:46:39
【问题描述】:

我有这些路线:

Auth::routes();
Route::get('/home', 'LibraryController@home');
Route::get('/', 'LibraryController@index');    

Auth::routes() 由命令php artisan make::auth 生成。但是,我不希望索引页面位于 auth 中间件组下,即上面列表中的第三条路由。

这里是控制器方法。 index() 适用于所有人,home() 适用于经过身份验证的用户。

 public function index()
    {
        return view('index');
    }

    public function home()
    {
        return view('home')->with('message','Logged in!');
    }

登录用户被重定向到主页:

protected $redirectTo = '/home';

但每当我运行第三条路线时,登录页面就会出现。那么,我如何从身份验证中间件组中删除该路由。

【问题讨论】:

  • 请稍微解释一下你的问题,不清楚你在问什么
  • 你想直接去首页而不是索引页面??
  • @recoverymen 没有。我不希望 Route::get('/', 'LibraryController@index'); 路由在 auth 中间件下。
  • 你想让LibraryController@index这里的索引方法不应该被认证意味着每个人都可以访问吗?这是你的问题吗?
  • 除非您从库控制器构造函数调用 auth 中间件方法,否则我在 auth 中间件下看不到任何路由。如果是这样,请删除该方法。

标签: laravel laravel-5.3


【解决方案1】:

在您的 LibraryController 中,在您的控制器开始索引之前,您需要编写

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

现在每个用户都可以在没有身份验证的情况下使用索引方法

文档参考https://laravel.com/docs/5.0/controllers#controller-middleware

【讨论】:

    【解决方案2】:

    从 Laravel 7.7 开始,您可以使用 excluded_middleware 属性,例如:

    Route::group([
      'excluded_middleware' => ['auth'],
    ], function () {
      Route::get('/home', 'LibraryController@home');
      Route::get('/', 'LibraryController@index');   
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2023-03-29
      • 1970-01-01
      • 2018-04-27
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多