从 Auth::routes() 中删除一条路线
Removing a route from Auth::routes()
我有这些路线:
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!');
}
登录用户被重定向到主页url:
protected $redirectTo = '/home';
但每当我 运行 第三条路线时,登录页面就会出现。那么,我如何从 auth 中间件组中删除该路由。
在您的 LibraryController 中,在您的控制器开始的索引之前,您需要编写
public function __construct()
{
$this->middleware('auth', ['except' => ['index']]);
}
现在每个用户都可以在没有身份验证的情况下进入索引方法
文档参考https://laravel.com/docs/5.0/controllers#controller-middleware
因为 Laravel 7.7 您可以使用 excluded_middleware
属性 例如:
Route::group([
'excluded_middleware' => ['auth'],
], function () {
Route::get('/home', 'LibraryController@home');
Route::get('/', 'LibraryController@index');
});
我有这些路线:
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!');
}
登录用户被重定向到主页url:
protected $redirectTo = '/home';
但每当我 运行 第三条路线时,登录页面就会出现。那么,我如何从 auth 中间件组中删除该路由。
在您的 LibraryController 中,在您的控制器开始的索引之前,您需要编写
public function __construct()
{
$this->middleware('auth', ['except' => ['index']]);
}
现在每个用户都可以在没有身份验证的情况下进入索引方法
文档参考https://laravel.com/docs/5.0/controllers#controller-middleware
因为 Laravel 7.7 您可以使用 excluded_middleware
属性 例如:
Route::group([
'excluded_middleware' => ['auth'],
], function () {
Route::get('/home', 'LibraryController@home');
Route::get('/', 'LibraryController@index');
});