从Auth :: routes()中删除路由

时间:2016-12-28 11:43:57

标签: laravel laravel-5.3

我有这些路线:

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';

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

2 个答案:

答案 0 :(得分:2)

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

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

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

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

答案 1 :(得分:0)

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

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