在Laravel中使用Middleware進行身份驗證


新建一個中間件:

方法寫在handle中

 判斷用戶是否登錄而且是否是管理員,不是的話返回到主頁

新建判斷是否為管理員的方法

 在kernel定義一個中間件,key是admin

注冊群組路由:prefix是路由前綴,訪問路由會自動在前面加上路由前綴;middleware是key值,會去驗證中間件

1在數據庫中是管理員

成功

 

附:

注冊單個路由的中間件:

Route::get('profile', [ 'middleware' => 'auth', 'uses' => 'ProfileController@show' ]);

為一個路由注冊多個中間件

Route::get('/', ['middleware' => ['first', 'second'], function () {
    //
}]);

 多個路由

Route :: group( [ 'middleware' => 'auth' ] , function( ){
            Route::get('/guarantee', 'Home\UserController@guarantee');
            Route::get('/securitycode', 'Home\UserController@securityCode');
    });

 

 

 

 整理自:www.laravist.com 視頻教程


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM