laravelÏîÄ¿ÖÐÔõÑù°ó¶¨Â·ÓÉ
laravelÊÇÒ»¸öʹÓÃphpÓïÑÔ±àдµÄ¿ªÔ´¿ò¼Ü£¬Ëü¿ÉÒÔÓÃÓÚ¿ìËÙ¿ª·¢¸ßÆ·ÖʵÄwebÓ¦ÓóÌÐò¡£ÔÚlaravel¿ò¼ÜÖУ¬Â·ÓÉÊǽ«url£¨uniform resource locator£©Ó³Éäµ½Ó¦ÓóÌÐòÖиø¶¨µÄ²Ù×÷µÄÒªÁì¡£
ÔÚLaravelÖУ¬½«URLºÍÓ¦ÓóÌÐò²Ù×÷Ó³ÉäµÄÀú³Ì³ÆΪ·Óɰ󶨡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ñ§Ï°ÔÚLaravelÓ¦ÓóÌÐòÖÐÔõÑù°ó¶¨Â·ÓÉ¡£
Ò»¡¢»ù±¾Â·ÓÉ°ó¶¨
ÔÚLaravelÖнç˵·ÓÉ£¬ÎÒÃÇ¿ÉÒÔʹÓÃRoute facadeÌṩµÄÒªÁì¡£Route::get(), Route::post()£¬Route::put()ºÍRoute::delete()µÈÒªÁì·ÓɵÄHTTPÇëÇóÀàÐÍ¡£
ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂ뽨ÉèÒ»¸ö»ù±¾Â·ÓÉ£º
Route::get('/', function () { return 'Hello World'; });
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂë½ç˵ÁËÒ»¸ö»ù±¾Â·ÓÉ£¬ÓÃÓÚ´¦Àí¸ùURL£¨/£©µÄHTTP GETÇëÇó¡£µ±Óû§»á¼û¸ùURLʱ£¬Laravel½«·µ»ØÒ»¸ö¡°Hello World¡±×Ö·û´®¡£
¶þ¡¢Â·ÓɲÎÊý°ó¶¨
ÏÖÔÚÈÃÎÒÃÇ¿´¿´ÔõÑùÔÚLaravelÖа󶨴ø²ÎÊýµÄ·ÓÉ¡£
ÎÒÃÇʹÓÃ{parameter}À´ÌåÏÖÒ»¸ö·ÓɲÎÊý¡£ÀýÈ磬ÈÃÎÒÃǽ¨ÉèÒ»¸ö´øÓÐURL²ÎÊýµÄ·ÓÉ¡£
Route::get('/hello/{name}', function ($name) { return 'Hello ' . $name; });
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪ¡°name¡±µÄURL²ÎÊý¡£µ±Óû§»á¼û/hello/{name} URLʱ£¬Laravel½«°Ñ²ÎÊýÃû³ÆÓ³É䵽ת´ï¸ø´¦Àí³ÌÐòµÄ²ÎÊýÖС£
Èý¡¢ÃüÃû·ÓÉ°ó¶¨
ÓÐʱ£¬ÎÒÃÇÐèҪΪһ¸öÌض¨µÄ·ÓÉ·ÖÅÉÒ»¸öÃû³Æ¡£ÎÒÃÇ¿ÉÒÔʹÓÃÃüÃû·ÓÉ°ó¶¨À´ÎªÂ·ÓÉ·ÖÅÉÃû³Æ£¬ÕâÔÚÖع¹´úÂëʱºÜÊÇÓÐÓá£
ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëΪ·ÓÉ·ÖÅÉÃû³Æ£º
Route::get('/about', function () { return 'This is the about page'; })->name('about');
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃname()ÒªÁ콫·ÓÉ·ÖÅÉÒ»¸öÃû³Æ£¬Ãû³ÆΪ¡°about¡±¡£
ËÄ¡¢Â·ÓÉ·Ö×é
·ÓÉ·Ö×éÊÇÉùÃ÷ÔÚͳһ·Ö×éÖеĶà¸ö·ÓɵÄÊÖÒÕ¡£ÎÒÃÇ¿ÉÒÔʹÓ÷Ö×éµ¥¶À½ç˵·ÓÉÆ÷µÄ´¦Àí³ÌÐò£¬ÖÐÐļþ»òǰ׺¡£
ÒÔÏÂÊÇÒ»¸öÓÃÓÚ·ÓÉ·Ö×éµÄʾÀý£º
Route::prefix('admin')->group(function () { Route::get('dashboard', function () { return 'This is the admin dashboard'; }); Route::get('users', function () { return 'This is the admin users page'; }); });
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÊ×ÏÈʹÓÃprefix()ÒªÁìΪ·ÓÉÆ÷Ìí¼Óǰ׺¡£È»ºóÎÒÃǽ«Á½¸ö·ÓÉ°üÀ¨ÔÚÒ»¸ö·Ö×éÖУ¬½ç˵ÁËÒ»¸öURI£¨/admin/dashboard£©ºÍÒ»¸öÓÃÓÚÓû§Êý¾ÝµÄURI£¨/admin/users£©¡£
Î塢·ÓÉÖÐʹÓÿØÖÆÆ÷
ÔÚLaravelÖУ¬¿ØÖÆÆ÷ÊÇÒ»¸öÀ࣬ËüÈÏÕæ´¦ÀíÇëÇó²¢·µ»ØÏìÓ¦¡£Ê¹ÓÿØÖÆÆ÷¿ÉÒÔ½«´úÂëÂß¼´Ó·ÓÉÎļþÖÐÊèÉ¢³öÀ´£¬Ê¹´úÂëÔ½·¢Ä£¿é»¯¡£
ÒÔÏÂÊÇÒ»¸öÔÚLaravelÓ¦ÓóÌÐòÖÐʹÓÿØÖÆÆ÷µÄʾÀý£º
Route::get('/hello/{name}', 'HelloController@index');
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂë½ç˵ÁËÒ»¸ö·ÓÉ£¬µ±Óû§»á¼ûURL/hello/{name}ʱ£¬½«Å²ÓÃHelloController¿ØÖÆÆ÷µÄindex()ÒªÁì¡£ÔÚÕâ¸öʾÀýÖУ¬$name²ÎÊý½«×÷Ϊ¿ØÖÆÆ÷ÒªÁìµÄ²ÎÊýת´ï¡£
Áù¡¢Â·ÓÉÖÐʹÓÃÖÐÐļþ
ÔÚLaravelÖУ¬ÖÐÐļþÊÇÔÚÇëÇóºÍÏìÓ¦Ö®¼äÖ´ÐеÄ×é¼þ¡£ÖÐÐļþ¿ÉÒÔÓÃÓÚÑéÖ¤ÇëÇ󣬼ì²éÓû§ÊÇ·ñÒѵǼ£¬¿ØÖÆ»á¼ûµÈ¡£
ÒÔÏÂÊÇÒ»¸öÔÚLaravelÓ¦ÓóÌÐòÖÐʹÓÃÖÐÐļþµÄʾÀý£º
Route::get('/dashboard', function () { return 'This is the dashboard page'; })->middleware('auth');
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂë½ç˵ÁËÒ»¸ö·ÓÉ£¬µ±Óû§»á¼ûURL /dashboardʱ£¬½«Ê¹ÓÃauthÖÐÐļþ¾ÙÐÐÉí·ÝÑéÖ¤£¬ÒÔÈ·±£Óû§ÒѵǼ¡£ÈôÊÇÓû§Î´Í¨¹ýÑéÖ¤£¬Laravel½«×Ô¶¯Öض¨Ïòµ½ÄúµÄÓ¦ÓóÌÐòÖеĵǼҳÃæ¡£
Æß¡¢½áÂÛ
LaravelÌṩÁ˼òÆÓ£¬ÎÞаÇÒÇ¿Ê¢µÄ·ÓÉ°ó¶¨ÊÖÒÕ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËһЩ»ù±¾µÄ·ÓÉ°ó¶¨ÊÖÒÕ£¬Èç½ç˵»ù±¾Â·ÓÉ£¬Â·ÓɲÎÊý£¬ÃüÃû·ÓÉ£¬Â·ÓÉ·Ö×飬¿ØÖÆÆ÷ºÍÖÐÐļþ£¬Äú¿ÉÒÔʹÓÃÕâЩÊÖÒÕ¹¹½¨ÖØ´óµÄWebÓ¦ÓóÌÐòºÍAPI¡£
Ï£ÍûÕâƪÎÄÕ¶ÔÄúÓÐËù×ÊÖú£¬ÈÃÄú¸üºÃµØÏàʶLaravelÖзÓɰ󶨵ÄÖÖÖÖÓ÷¨ºÍÊÖÒÕ¡£
ÒÔÉϾÍÊÇlaravelÏîÄ¿ÖÐÔõÑù°ó¶¨Â·ÓɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡