ÔõÑùÔÚLaravel 5.4ÖÐʵÏÖÖÐÐļþÌøת
ÔÚlaravel 5.4֮ǰ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÖÐÐļþÀ´ÊµÏÖÌøת¹¦Ð§¡£¿ÉÊÇ£¬laravel 5.4ÖÐÐļþµÄʵÏÖ·½·¨ÓÐËù²î±ð¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÌÖÂÛÔõÑùÔÚlaravel 5.4ÖÐʵÏÖÖÐÐļþÌøת¡£
Ò»¡¢ÔÚLaravel 5.4ÖÐʵÏÖÖÐÐļþÌøת
ÔÚLaravel 5.4ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃredirect()º¯ÊýºÍwith()º¯ÊýÀ´ÊµÏÖÖÐÐļþÌøת¡£ÏÂÃæÎÒÃÇÀ´¿´Ò»¸öÀý×Ó£º
½¨ÉèÒ»¸öÈÕÖ¾ÖÐÐļþ
ÔÚapp/Http/MiddlewareĿ¼Ï£¬½¨ÉèÒ»¸öLogMiddleware.phpÎļþ£¬ÄÚÈÝÈçÏ£º
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Log; class LogMiddleware { public function handle($request, Closure $next) { Log::info('LogMiddleware'); return $next($request); } }
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽨ÉèÁËÒ»¸öÃûΪLogMiddlewareµÄÖÐÐļþ£¬ÔÚhandleº¯ÊýÖмͼÁËÒ»ÌõÈÕÖ¾¡£
½¨ÉèÒ»¸öÊÚȨÖÐÐļþ
ÔÚapp/Http/MiddlewareĿ¼Ï£¬½¨ÉèÒ»¸öAuthMiddleware.phpÎļþ£¬ÄÚÈÝÈçÏ£º
<?php namespace App\Http\Middleware; use Closure; class AuthMiddleware { public function handle($request, Closure $next) { if (!auth()->check()) { return redirect('/login')->with('error', 'ÇëÏȵǼ£¡'); } return $next($request); } }
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽨ÉèÁËÒ»¸öÃûΪAuthMiddlewareµÄÖÐÐļþ£¬ÔÚhandleº¯ÊýÖÐÅжÏÓû§ÊÇ·ñÒѵǼ£¬ÈôÊÇδµÇ¼£¬ÔòÌøתµ½µÇ¼ҳÃ棬²¢½«¹ýʧÐÅÏ¢ÉúÑĵ½sessionÖС£
½«ÖÐÐļþ×¢²áµ½Â·ÓÉÖÐ
ÔÚroutes/web.phpÎļþÖУ¬×¢²á·ÓɺÍÖÐÐļþ£¬´úÂëÈçÏ£º
Route::get('/dashboard', function () { return view('dashboard'); })->middleware([LogMiddleware::class, AuthMiddleware::class]);
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽨ÉèÁËÒ»¸öÃûΪdashboardµÄ·ÓÉ£¬ÔÚdashboard·ÓÉÖУ¬Ê¹ÓÃÁËLogMiddlewareºÍAuthMiddlewareÖÐÐļþ¡£
ʵÏÖÖÐÐļþÌøת
Ö±½Ó»á¼ûhttp://yourdomain/dashboardÒ³Ãæʱ£¬½«»á×Ô¶¯Ìøתµ½http://yourdomain/login£¬²¢ÔÚsessionÖÐÉúÑÄÓйýʧÐÅÏ¢¡£
¶þ¡¢×ܽá
ÔÚLaravel 5.4ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃredirect()º¯ÊýºÍwith()º¯ÊýÀ´ÊµÏÖÖÐÐļþÌøת¡£Í¨¹ýÒÔÉÏ°ì·¨£¬ÎÒÃÇÀֳɵØʵÏÖÁËÒ»ÆäÖÐÐļþÌøת¹¦Ð§¡£Ï£Íû±¾ÎÄÄܶÔÄãÓÐËù×ÊÖú£¡
ÒÔÉϾÍÊÇÔõÑùÔÚLaravel 5.4ÖÐʵÏÖÖÐÐļþÌøתµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡