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ÖÐÑë¼þÈÏÕæʲôµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡