laravel ÅжÏÇëÇóÒ³Ãæ
laravel×÷Ϊһ¿îÊ¢ÐеÄphp¿ò¼Ü£¬Æä×î´óµÄÌصãÊÇÆäÎÞаÐÔºÍÒ×ÓÃÐÔ¡£ÔÚ¿ª·¢Àú³ÌÖУ¬ÔõÑùÅжÏÇëÇóµÄÒ³ÃæÊÇÎÒÃÇÐèÒªÒ»Ö±¹Ø×¢µÄ»°Ìâ¡£±¾ÎĽ«Õë¶ÔlaravelÖÐÅжÏÇëÇóÒ³ÃæµÄÏà¹ØÒªÁì×öÒ»¸ö¼òÆÓµÄÏÈÈÝ¡£
Ò»¡¢ÅжÏÇëÇóµÄÒªÁì
1.1¡¢Ê¹ÓÃRequestÀàµÄisMethodÒªÁì
LaravelµÄRequestÀàµÄisMethodÒªÁì¿ÉÓÃÓÚÅжÏÇëÇóµÄÒªÁì¡£ÀýÈ磺
use IlluminateHttpRequest; Route::post('/submit', function (Request $request) { if ($request->isMethod('post')) { //Ìá½»±íµ¥ } });
µÇ¼ºó¸´ÖÆ
1.2¡¢Ê¹ÓÃRequestÀàµÄÒªÁìÃû
³ýÁËisMethodÒªÁ죬»¹¿ÉÒÔʹÓÃRequestÀàµÄ¶ÔÓ¦ÒªÁìÃû¾ÙÐÐÅжϣ¬ÀýÈ磺
use IlluminateHttpRequest; Route::post('/submit', function (Request $request) { if ($request->isPost()) { //Ìá½»±íµ¥ } });
µÇ¼ºó¸´ÖÆ
×¢ÖØ£º¹ØÓÚPUT¡¢PATCHºÍDELETEÇëÇó£¬Ê¹ÓÃÉÏÊöÒªÁìʱÐèÒªÔÚ±íµ¥ÖмÓÈë_method×ֶΣ¬ÏêϸҪÁì¿É²Î¿¼Laravel¹Ù·½Îĵµ¡£
¶þ¡¢ÅжÏÇëÇóµÄ·ÓÉ
2.1¡¢Ê¹ÓÃRequestÀàµÄrouteIsÒªÁì
LaravelµÄRequestÀàµÄrouteIsÒªÁì¿ÉÒÔÓÃÓÚÅжÏÄ¿½ñ·ÓÉÊÇ·ñÓëÖ¸¶¨µÄ·ÓÉÃûÆ¥Å䣬ÀýÈ磺
use IlluminateHttpRequest; Route::get('/page', function (Request $request) { if ($request->routeIs('page')) { //Ö´ÐÐÏà¹Ø²Ù×÷ } })->name('page');
µÇ¼ºó¸´ÖÆ
2.2¡¢Ê¹ÓÃRouteÀàµÄcurrentRouteNameÒªÁì
LaravelµÄRouteÀàµÄcurrentRouteNameÒªÁì¿ÉÒÔÓÃÓÚ»ñÈ¡Ä¿½ñ·ÓɵÄÃû³Æ£¬ÀýÈ磺
use IlluminateSupportFacadesRoute; Route::get('/page', function () { $currentRouteName = Route::currentRouteName(); if ($currentRouteName == 'page') { //Ö´ÐÐÏà¹Ø²Ù×÷ } })->name('page');
µÇ¼ºó¸´ÖÆ
Èý¡¢ÅжÏÇëÇóµÄURL
3.1¡¢Ê¹ÓÃRequestÀàµÄisÒªÁì
LaravelµÄRequestÀàµÄisÒªÁì¿ÉÒÔÓÃÓÚÅжÏÄ¿½ñURLÊÇ·ñÆ¥ÅäÖ¸¶¨µÄURL£¬ÀýÈ磺
use IlluminateHttpRequest; Route::get('/page', function (Request $request) { if ($request->is('/page')) { //Ö´ÐÐÏà¹Ø²Ù×÷ } });
µÇ¼ºó¸´ÖÆ
3.2¡¢Ê¹ÓÃRequestÀàµÄfullUrlIsÒªÁì
LaravelµÄRequestÀàµÄfullUrlIsÒªÁì¿ÉÒÔÓÃÓÚÅжÏÄ¿½ñÍêÕûµÄURLÊÇ·ñÆ¥ÅäÖ¸¶¨µÄURL£¬ÀýÈ磺
use IlluminateHttpRequest; Route::get('/page', function (Request $request) { if ($request->fullUrlIs('http://localhost/page')) { //Ö´ÐÐÏà¹Ø²Ù×÷ } });
µÇ¼ºó¸´ÖÆ
ËÄ¡¢×ܽá
±¾ÎļòÆÓÏÈÈÝÁËLaravelÖÐÅжÏÇëÇóÒ³ÃæµÄÏà¹ØÒªÁ죬°üÀ¨ÅжÏÇëÇóµÄÒªÁì¡¢ÅжÏÇëÇóµÄ·ÓɺÍÅжÏÇëÇóµÄURL¡£ÔÚÏÖʵ¿ª·¢Àú³ÌÖУ¬ÎÒÃÇ¿ÉÒÔƾ֤ÐèҪѡÔñ²î±ðµÄÒªÁì¾ÙÐÐÅжϣ¬ÒÔʵÏÖÔ½·¢ÎÞаºÍ׼ȷµÄ¿ØÖÆ¡£
ÒÔÉϾÍÊÇlaravel ÅжÏÇëÇóÒ³ÃæµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡