laravel5»ñÈ¡¿ØÖÆÆ÷ÒªÁì
ÔÚ laravel 5 ÖУ¬»ñÈ¡¿ØÖÆÆ÷ÒªÁìÊÇÒ»¸ö·Ç³£»ù±¾¶øÓÖ³£ÓõIJÙ×÷¡£ÔÚijЩ³¡¾°Ï£¬ÎÒÃÇ¿ÉÄÜÐèÒª»ñÈ¡Ä¿½ñ¿ØÖÆÆ÷Ö´ÐеÄÒªÁìÃû³Æ¡£ÄÇô£¬ÔÚ laravel 5 ÖУ¬ÔõÑù»ñÈ¡¿ØÖÆÆ÷ÒªÁìÄØ£¿½ÓÏÂÀ´£¬ÈÃÎÒÃÇÒ»ÆðÀ´ÏàʶһÏ¡£
1¡¢$route ¹¤¾ß
ÔÚ Laravel 5 ÖУ¬$route ¹¤¾ßÉúÑÄÁËÄ¿½ñÕýÔÚÖ´ÐпØÖÆÆ÷µÄ·ÓÉÐÅÏ¢¡£Õâ¸ö¹¤¾ß°üÀ¨ÁËÇëÇóµÄ URI¡¢ÇëÇóÒªÁìÒÔ¼°Â·ÓɲÎÊýµÈ¡£ÈôÊÇÒª»ñÈ¡Ä¿½ñ¿ØÖÆÆ÷µÄÒªÁìÃû£¬¿ÉÒÔͨ¹ý $route ¹¤¾ßµÄ ¡°getActionName¡± ÒªÁìÀ´»ñÈ¡¡£
/** »ñÈ¡Ä¿½ñ¿ØÖÆÆ÷ÒªÁìÃû³Æ **/ public function getCurrentMethodName() { $route = Route::getCurrentRoute(); $action = $route->getActionName(); $fragments = explode('@', $action); $method = end($fragments); return $method; }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃÇÊ×ÏÈͨ¹ý Route::getCurrentRoute() ÒªÁì»ñÈ¡Ä¿½ñµÄ·Óɹ¤¾ß£¬È»ºóʹÓà $route ¹¤¾ßµÄ ¡°getActionName¡± ÒªÁìÀ´»ñÈ¡Ä¿½ñ¿ØÖÆÆ÷ÒªÁìµÄÍêÕû·¾¶£¬ÀýÈç ¡°AppHttpControllersTestController@index¡±¡£½Ó×Å£¬ÎÒÃÇʹÓà explode() º¯Êý½ØÈ¡³öÒªÁìÃû³Æ²¿·Ö£¬²¢Ê¹Óà end() º¯Êý»ñÈ¡Êý×éµÄ×îºóÒ»¸öÔªËØ£¬¼´¿ØÖÆÆ÷µÄÒªÁìÃû¡£×îºó£¬½«ÒªÁìÃû·µ»Ø¼´¿É¡£
2¡¢·´Éä
³ýÁËʹÓà $route ¹¤¾ßÍ⣬ÎÒÃÇ»¹¿ÉÒÔͨ¹ý PHP ·´Éä»úÖÆÀ´»ñÈ¡¿ØÖÆÆ÷ÒªÁì¡£PHP ·´Éä»úÖÆÌṩÁËÒ»¸öÀà ReflectionClass£¬¿ÉÓÃÓÚ»ñÈ¡ÀàµÄÏà¹ØÐÅÏ¢£¬°üÀ¨ÒªÁì¡¢ÊôÐÔ¡¢³£Á¿µÈ¡£Í¨¹ý ReflectionClass£¬ÎÒÃÇ¿ÉÒÔ»ñÈ¡Ä¿½ñ¿ØÖÆÆ÷µÄÒªÁìÃû³Æ¡£
/** »ñÈ¡Ä¿½ñ¿ØÖÆÆ÷ÒªÁìÃû³Æ **/ public function getCurrentMethodName() { $controller = Route::current()->getController(); $reflection = new ReflectionClass($controller); $method = $reflection->getMethod(Route::currentRouteAction()); return $method->name; }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃÇÊ×ÏÈ»ñÈ¡Ä¿½ñ¿ØÖÆÆ÷µÄʵÀý¹¤¾ß£¬È»ºóʹÓà ReflectionClass Àà»ñÈ¡¸ÃʵÀý¹¤¾ßµÄ·´ÉäÐÅÏ¢¡£½Ó×Å£¬Í¨¹ý·´ÉäʵÀýµÄ getMethod() ÒªÁì»ñÈ¡Ä¿½ñÖ´ÐеÄÒªÁì¡£×îºó£¬Ê¹Ó÷´ÉäÒªÁìµÄ name ÊôÐÔ»ñÈ¡ÒªÁìÃû³Æ²¢·µ»Ø¼´¿É¡£
×ܽá
ÒÔÉϾÍÊÇÔÚ Laravel 5 ÖлñÈ¡¿ØÖÆÆ÷ÒªÁìµÄÁ½ÖÖÒªÁì¡£ÎÞÂÛÊÇÄÄÖÖ·½·¨£¬¶¼»áΪÎÒÃÇÌṩ·Ç³£¿ì½Ý¡¢Àû±ãµÄ²Ù×÷·½·¨£¬ÈÃÎÒÃǸüºÃµØÕÆ¿Ø¿ØÖÆÆ÷µÄÖ´ÐÐÇéÐΡ£ÈôÊÇÄã¶Ô Laravel 5 µÄ¿ª·¢ÓÐËùÒÉÎÊ»òÕßÐèҪѧϰ¸ü¶àÓÐ¹Ø Laravel 5 µÄÄÚÈÝ£¬ÇëÉó²é¹Ù·½Îĵµ»òÕ̡߽̳£ÕÆÎÕ Laravel 5 ¿ª·¢ÄÜÁ¦£¬½«ÖúÄãÔÚ Web ¿ª·¢ÁìÓòÖÐѸËÙ»ñµÃÓÅÊÆ¡£
ÒÔÉϾÍÊÇlaravel5»ñÈ¡¿ØÖÆÆ÷ÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡