将 Auth 中间件应用于所有 Laravel 路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54198483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apply Auth Middleware to All Laravel Routes
提问by user3351236
What is the correct way to authenticate all routes except login and register when I apply auth middleware in all controllers? Is there a way to apply auth middleware in one place and exclude login, register routes?
当我在所有控制器中应用身份验证中间件时,除了登录和注册之外,对所有路由进行身份验证的正确方法是什么?有没有办法在一个地方应用身份验证中间件并排除登录、注册路由?
回答by Djellal Mohamed Aniss
you can apply middlewares in the routes.php file, what you need to do is to put all your routes on a group, and add the middleware 'auth' ( except the Auth::routes() which are already configured), for example :
你可以在routes.php文件中应用中间件,你需要做的是将你所有的路由放在一个组中,并添加中间件'auth'(除了已经配置的Auth::routes()),例如:
Route::middleware(['first', 'second'])->group(function () {
Route::get('/', function () {
// Uses first & second Middleware
});
Route::get('user/profile', function () {
// Uses first & second Middleware
});
});
more information can be found in the docs: https://laravel.com/docs/5.7/routing#route-group-middleware
更多信息可以在文档中找到:https: //laravel.com/docs/5.7/routing#route-group-middleware
回答by Khan Shahrukh
You can group all your authenticated routes like following, laravel provides a default middleware for auth and guest users
您可以将所有经过身份验证的路由分组如下,laravel 为 auth 和来宾用户提供默认中间件
Route::group(['middleware' => ['auth']], function () {
Route::get('home', 'HomeController@index');
Route::post('save-user', 'UserController@saveUser');
Route::put('edit-user', 'UserController@editUser');
});
The above route names are just made up, please follow a proper naming convention for your routes and controllers. Also read about middlewares over hereand about routing over here
回答by emotality
You can add middleware to your whole web.php
route file by adding the middleware to your routes mapping in RouteServiceProvider
.
您可以web.php
通过将中间件添加到RouteServiceProvider
.
Go to app/Providers/RouteServiceProvider.php
and in mapWebRoutes()
, change middleware('web')
to middleware(['web', 'auth'])
:
转到app/Providers/RouteServiceProvider.php
并在mapWebRoutes()
,更改middleware('web')
为middleware(['web', 'auth'])
:
protected function mapWebRoutes()
{
Route::middleware(['web', 'auth'])
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
Create a new method mapAuthRoutes()
:
创建一个新方法mapAuthRoutes()
:
protected function mapAuthRoutes()
{
Route::middleware('web')
->namespace('App\Http\Controllers\Auth')
->group(base_path('routes/auth.php'));
}
Map it:
映射它:
public function map()
{
$this->mapAuthRoutes(); // <-- add this
$this->mapWebRoutes();
...
}
Create an auth.php
file in your routes
folder, then paste the following (and remove unwanted stuff):
auth.php
在您的routes
文件夹中创建一个文件,然后粘贴以下内容(并删除不需要的内容):
<?php
use Illuminate\Support\Facades\Route;
Route::get('login', 'LoginController@showLoginForm')->name('login');
Route::post('login', 'LoginController@login')->name('login');
Route::post('logout', 'LoginController@logout')->name('logout');
Route::get('register', 'RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'RegisterController@register')->name('register');
Route::get('password/reset', 'ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'ResetPasswordController@reset')->name('password.update');
Route::get('email/verify', 'VerificationController@show')->name('verification.notice');
Route::get('email/verify/{id}', 'VerificationController@verify')->name('verification.verify');
Route::get('email/resend', 'VerificationController@resend')->name('verification.resend');
Now you can configure everything in 1 place, like prefix
, name
, middleware
and namespace
.
现在你可以在1米的地方,如配置一切prefix
,name
,middleware
和namespace
。
Check php artisan route:list
to see the results :)
检查php artisan route:list
以查看结果:)