將 Auth 中間件應用於所有 Laravel 路由 (Apply Auth Middleware to All Laravel Routes)


問題描述

將 Auth 中間件應用於所有 Laravel 路由 (Apply Auth Middleware to All Laravel Routes)

當我在所有控制器中應用身份驗證中間件時,對除登錄和註冊之外的所有路由進行身份驗證的正確方法是什麼?有沒有辦法在一個地方應用身份驗證中間件並排除登錄、註冊路由?


參考解法

方法 1:

You can group all your authenticated routes like following, laravel provides a default middleware for auth and guest users

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 here and about routing over here

方法 2:

You can add middleware to your whole web.php route file by adding the middleware to your routes mapping in RouteServiceProvider.

Go to app/Providers/RouteServiceProvider.php and in mapWebRoutes(), change middleware('web') to middleware(['web', 'auth']):

protected function mapWebRoutes()
{
    Route::middleware(['web', 'auth'])
         ‑>namespace($this‑>namespace)
         ‑>group(base_path('routes/web.php'));
}

This is (not?) totally unrelated but here's an example of a clean way to handle a lot of route files instead of throwing all your routes into a single web.php file:

Create a new method mapAdminRoutes():

protected function mapAdminRoutes()
{
    Route::middleware(['web', 'auth:admin'])
        ‑>namespace('App\Http\Controllers\Admin')
        ‑>name('admin.')
        ‑>group(base_path('routes/admin.php'));
}

Map it:

public function map()
{
    $this‑>mapWebRoutes();
    $this‑>mapAdminRoutes(); // <‑‑ add this
    ...
}

Create an admin.php file in your routes folder, then create your routes for Admin:

<?php

use Illuminate\Support\Facades\Route;

// This route's name will be 'admin.dashboard'
Route::get('dashboard', 'DashboardController@dashboard')‑>name('dashboard');

// This route's name will be 'admin.example'
Route::get('example', 'ExampleController@example')‑>name('example');

...

Now you can configure everything in 1 place, like prefix, name, middleware and namespace.

Check php artisan route:list to see the results :)

方法 3:

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 :

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

(by user3351236Khan ShahrukhemotalityDjellal Mohamed Aniss)

參考文件

  1. Apply Auth Middleware to All Laravel Routes (CC BY‑SA 2.5/3.0/4.0)

#laravel-middleware #laravel-5 #Laravel #laravel-routing






相關問題

使用中間件進行表操作 laravel 5.1 (Using middleware for table manipulation laravel 5.1)

Laravel 中間件“僅”為每條路線觸發 (Laravel middlware 'only' fires for every route)

在 laravel 5.1 中檢查管理員角色 (Checking admin roles in laravel 5.1)

在laravel 5中使用中間件重定向循環 (redirect loop with middleware in laravel 5)

Laravel - 中間件後清空 $request (Laravel - Empty $request after middleware)

$request->user()->role 錯誤 - 試圖獲取非對象的屬性 ($request->user()->role errror - trying to get property of non-object)

檢測到 Laravel 中間件但未執行 (Laravel middleware detected but not executed)

將 Auth 中間件應用於所有 Laravel 路由 (Apply Auth Middleware to All Laravel Routes)

路由組內的功能 [laravel-passport] (Function inside route group [laravel-passport])

當我嘗試訪問主頁時,Laravel 不會重定向到登錄頁面 (Laravel does not redirect to login page when I try to access to home page)

控制器沒有來自中間件的更改請求 (Controller doesnt have the alter request from the middleware)

訂閱者中間件路由允許公眾查看所有受限頁面 (Subscriber middleware route allowing public to view all restricted pages)







留言討論