laravelÖÐÐļþÈÏÕæʲô
laravel ÊÇÒ»¿îÊ¢ÐÐµÄ php ¿ªÔ´¿ò¼Ü£¬Í¨³£ÓÃÓÚ¿ª·¢ web Ó¦ÓóÌÐò¡£ËüÌṩÁËһϵÁй¤¾ßºÍ¹¦Ð§£¬¿ÉÒÔÇáËɵع¹½¨¸ßÖÊÁ¿¡¢¿ÉÀ©Õ¹µÄÓ¦ÓóÌÐò¡£ÆäÖУ¬ÖÐÐļþÊÇ laravel µÄÖ÷Òª×é³É²¿·Ö£¬ËüÔÚÓ¦ÓóÌÐòÖÐÊÎÑÝÁ˺ÜÖ÷ÒªµÄ½ÇÉ«¡£
±¾ÎĽ«ÏÈÈÝ Laravel ÖÐÐļþµÄ»ù±¾¿´·¨¡¢×÷Óü°×ÅʵÏÖÔÀí£¬ÒÔ×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·ÖÐÐļþµÄ×÷ÓúÍÓ÷¨¡£
ʲôÊÇÖÐÐļþ£¿
ÖÐÐļþÊÇ Laravel ÖÐÒ»ÖÖ´¦Àí HTTP ÇëÇóºÍÏìÓ¦µÄ»úÖÆ£¬ËüÌṩÁËÒ»ÖÖÎÞаµÄ·½·¨À´´¦ÀíÓ¦ÓóÌÐòÖÐÐí¶à³£¼ûµÄʹÃü£¬ÈçÉí·ÝÑéÖ¤¡¢Êý¾Ý¹ýÂË¡¢»á»°ÖÎÀíµÈ¡£ÖÐÐļþͨ³£Î»ÓÚÇëÇó´¦ÀíÁ÷³ÌµÄÖÐÐÄλÖ㬿ÉÒÔ¶ÔÇëÇóºÍÏìÓ¦¾ÙÐйýÂ˺ÍÐ޸ġ£
ÔÚ Laravel ÖУ¬ÖÐÐļþ¿ÉÒÔ±»ÓÃÓÚÁ½ÖÖ³¡¾°£ºÈ«¾ÖÖÐÐļþºÍ·ÓÉÖÐÐļþ¡£È«¾ÖÖÐÐļþÊÇÔÚËùÓÐ HTTP ÇëÇó֮ǰ´¦ÀíµÄ£¬¶ø·ÓÉÖÐÐļþ½ö¶ÔÌض¨Â·ÓɵÄÇëÇó¾ÙÐд¦Àí¡£ÖÐÐļþ¿ÉÒÔÊÇÒ»¸ö±Õ°üº¯Êý¡¢Ò»¸öÀà»òÒ»¸öʵÏÖÁË handle ÒªÁìµÄÀàµÄʵÀý¡£
ÖÐÐļþµÄ×÷ÓÃ
ÊÚȨºÍÈÏÖ¤
ÖÐÐļþ¿ÉÒÔÓÃÀ´ÑéÖ¤Óû§µÄÉí·Ý¡¢¼ì²éÓû§ÊÇ·ñÓÐȨ»á¼ûijЩ×ÊÔ´»òÕß±ÜÃâÓû§¾ÙÐв»·¨²Ù×÷¡£ºÃ±È£¬ÔÚÓû§ÊµÑé»á¼ûÖÎÀíºǫ́»òÕß²éÕÒÃô¸ÐÊý¾Ýʱ£¬¿ÉÒÔͨ¹ý±àдÏìÓ¦µÄÖÐÐļþÀ´¾ÙÐÐÉí·ÝÑéÖ¤¡£
¼Í¼ÈÕÖ¾
ÖÐÐļþ»¹¿ÉÒÔÓÃÀ´¼ÍÈÎÃü»§µÄÐÐΪºÍϵͳµÄÔËÐÐÇéÐΡ£ºÃ±È£¬¿ÉÒÔ±àдһÆäÖÐÐļþ£¬ÓÃÓÚ¼ÍÈÎÃü»§ÔÚÒ»¸öÌض¨Ò³ÃæµÄʱ¼ä£¬»òÕ߼ͼËùÓеÄÇëÇóºÍÏìÓ¦µÄʱ¼äºÍ״̬¡£Õâ¿ÉÒÔ×ÊÖú¿ª·¢Ö°Ô±¸üºÃµØÏàʶϵͳµÄÔËÐÐÇéÐΣ¬²¢ÓÅ»¯Ó¦ÓóÌÐòµÄÐÔÄÜ¡£
ѹËõ»ººÍ´æ
ÖÐÐļþ»¹¿ÉÒÔÓÃÀ´Ñ¹Ëõ»ººÍ´æÏìÓ¦Êý¾Ý£¬ÒÔ¼ÓËÙÓû§»á¼ûËÙÂÊ¡£ºÃ±È£¬¿ÉÒÔʹÓÃÖÐÐļþѹËõËùÓеÄÏìÓ¦Êý¾Ý£¬ÒÔïÔÌ´«Êäʱ¼äºÍ´ø¿íÕ¼Ó㬻òÕßʹÓÃÖÐÐļþ»º´æһЩ¾²Ì¬Êý¾Ý£¬ÒÔïÔÌÊý¾Ý¿âÅÌÎÊ´ÎÊý¡£
Êý¾Ýת»»
ÖÐÐļþ¿ÉÒÔÓÃÀ´½«Öݪֲî±ðÃûÌõÄÊý¾Ý£¨Èç XML¡¢JSON¡¢CSV µÈ£©×ª»»ÎªÍ³Ò»µÄÊý¾ÝÃûÌ᣺ñȣ¬¿ÉÒÔ±àдһÆäÖÐÐļþ£¬½«ËùÓеÄÏìÓ¦Êý¾Ý¶¼×ª»»Îª JSON ÃûÌã¬ÒÔÀû±ã¿Í»§¶Ë¾ÙÐÐÆÊÎöºÍ´¦Àí¡£
ÖÐÐļþµÄʵÏÖ
ÔÚ Laravel ÖУ¬¿ÉÒÔͨ¹ý¼ÌÐø Illuminate\Http\Middleware\Middleware ÀàÀ´½¨Éè×Ô¼ºµÄÖÐÐļþ¡£ÖÐÐļþͨ³£°üÀ¨Ò»¸ö handle ÒªÁ죬ÓÃÀ´´¦ÀíÇëÇóºÍÏìÓ¦¡£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÖÐÐļþʾÀý£¬ÓÃÓÚ¼ì²éÓû§ÊÇ·ñÓÐȨÏÞ»á¼ûijЩҳÃ棺
<?php namespace App\Http\Middleware; use Closure; class CheckUserPermission { public function handle($request, Closure $next) { if (! $request->user()->hasPermissionTo('access-admin')) { return redirect('/login'); } return $next($request); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬handle ÒªÁìÎüÊÕÒ»¸ö $request ²ÎÊýºÍÒ»¸ö Closure ÀàÐ͵IJÎÊý $next£¬ÓÃÓÚ½«ÇëÇóת´ï¸øÏÂÒ»ÆäÖÐÐļþ»ò¿ØÖÆÆ÷¡£
½ÓÏÂÀ´£¬¿ÉÒÔ½«ÖÐÐļþ×¢²áµ½Ó¦ÓóÌÐòÖС£¿ÉÒÔͨ¹ý app/Http/Kernel.php ÎļþµÄ $middleware ÊôÐÔÀ´Ìí¼ÓÈ«¾ÖÖÐÐļþ£¬Í¨¹ý $routeMiddleware ÊôÐÔÀ´Ìí¼Ó·ÓÉÖÐÐļþ¡£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀý£¬×¢²áÉÏÃæµÄÖÐÐļþ£º
<?php namespace App\Http; use Illuminate\Foundation\Http\Kernel as HttpKernel; class Kernel extends HttpKernel { protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'permission' => \App\Http\Middleware\CheckUserPermission::class, ]; }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬½«ÖÐÐļþ CheckUserPermission ×¢²áΪ·ÓÉÖÐÐļþ permission¡£
¿ÉÒÔͨ¹ýÔÚ·ÓÉÉùÃ÷ÖÐʹÓÃÖÐÐļþÃû³ÆÀ´½«ÆäÓ¦ÓÃÓÚ·ÓÉ¡£ÏÂÃæµÄʾÀýÑÝʾÁËÔõÑùʹÓÃÖÐÐļþ½«»á¼û /admin ·ÓɵÄÇëÇóÏÞÖÆΪÓÐÖÎÀíԱȨÏÞµÄÓû§£º
Route::get('/admin', function () { // })->middleware('permission:access-admin');
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬½«Â·ÓÉÖÐÐļþ permission Ó¦ÓÃÓÚ /admin ·ÓÉ£¬²¢×ª´ï access-admin ²ÎÊý£¬Ö¸Ê¾Óû§ÐèÒª¾ßÓÐ access-admin ȨÏ޲Żª»á¼û¸Ã·ÓÉ¡£
×ܽá
ÖÐÐļþÊÇ Laravel ³ÌÐòÖеÄÒ»¸öÇ¿Ê¢»úÖÆ£¬¿ÉÒÔÔÚÇëÇó´¦ÀíÀú³ÌÖоÙÐÐÊý¾Ý¹ýÂË¡¢Éí·ÝÑéÖ¤¡¢ÈÕÖ¾¼Í¼¡¢»º´æµÈÖݪֲÙ×÷¡£Ëüͨ¹ý¿ÉÀ©Õ¹µÄ·½·¨¶Ô´¦ÀíÁ÷³Ì¾ÙÐзâ×°£¬²¢ÌṩÁËÀû±ã¡¢ÎÞаµÄʹÓ÷½·¨¡£
±¾ÎÄÏÈÈÝÁË Laravel ÖÐÐļþµÄ»ù±¾¿´·¨¡¢Ó¦Óó¡¾°ºÍʵÏÖÒªÁ죬ϣÍû¶Ô¶ÁÕßÏàʶÖÐÐļþ±³ºóµÄÔÀíºÍʹÓÃÒªÁìÓÐËù×ÊÖú¡£ÔÚÏÖʵ¿ª·¢ÖУ¬¿ÉÒÔƾ֤ÏêϸÐèÒª±àд×Ô¼ºµÄÖÐÐļþ£¬Ìá¸ßÓ¦ÓóÌÐòµÄ¿É¿¿ÐÔ¡¢¿ÉÀ©Õ¹ÐԺͿÉά»¤ÐÔ¡£
ÒÔÉϾÍÊÇlaravelÖÐÐļþÈÏÕæʲôµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡