laravel·ÓÉÉèÖÃÎļþÊÇʲô
laravel ·ÓÉÉèÖÃÎļþÊÇʲô
Laravel ÊÇÒ»¸öÊ¢ÐÐµÄ PHP ¿ª·¢¿ò¼Ü£¬ËüÌṩÁËÇ¿Ê¢µÄ·ÓÉϵͳ£¬¿ÉÒÔʵÏÖ URL µ½¿ØÖÆÆ÷ÒªÁìµÄÓ³Éä¡£Laravel µÄ·ÓÉϵͳ¿ÉÒÔÈÃÎÒÃÇÇáËɵؽ¨Éè RESTful API ºÍ Web Ó¦ÓóÌÐò£¬²¢ÇÒ¿ÉÒÔÈÃÎÒÃÇÒÔºÜÊǼòÆӵķ½·¨´¦ÀíÖÖÖÖ HTTP ÇëÇó£¨GET¡¢POST¡¢PUT¡¢DELETE µÈµÈ£©¡£ÔÚ Laravel ÖУ¬Â·ÓÉÓÉÒ»¸ö»ò¶à¸öɢװ·Óɽç˵×é³É£¬»¹¿ÉÒÔ½«ÕâЩ·ÓÉ×é³ÉÒ»¸ö·Ö×é·ÓÉ¡£±¾ÎĽ«ÖصãÏÈÈÝ Laravel ·ÓÉÉèÖÃÎļþ¡£
ÔÚ Laravel ÖУ¬Â·ÓÉ¿ÉÒÔÖ±½ÓÔÚ·ÓÉÎļþÖоÙÐнç˵£¬Ò²¿ÉÒÔʹÓ÷ÓÉÉèÖÃÎļþ¾ÙÐнç˵¡£ÄÇô£¬Â·ÓÉÉèÖÃÎļþÊÇʲôÄØ£¿¼òÆÓÀ´Ëµ£¬Laravel ·ÓÉÉèÖÃÎļþÊǽç˵·ÓÉµÄ PHP Îļþ£¬ÆäÖаüÀ¨Â·Óɽç˵¡¢Â·ÓÉ·Ö×éµÈÐÅÏ¢¡£½«Â·Óɽç˵·ÅÔÚÉèÖÃÎļþÖУ¬¿ÉÒÔÈÃÎÒÃÇͨ¹ý¼òÆÓÐÞ¸ÄÉèÖÃÎļþÀ´¸Ä±äÓ¦ÓóÌÐòµÄ·ÓÉ£¬¶ø²»ÐèÒªÐ޸ĿØÖÆÆ÷´úÂë¡£
Laravel µÄ·ÓÉÉèÖÃÎļþͨ³£ÉúÑÄÔÚ /routes Ŀ¼Ï£¬ÎļþÃûΪ web.php »ò api.php£¬»®·Ö¶ÔÓ¦ Web Ó¦ÓóÌÐòºÍ API Ó¦ÓóÌÐòµÄ·Óɽç˵¡£ÔÚÕâÁ½¸öÎļþÖУ¬Laravel ÌṩÁËһЩʾÀý´úÂ룬ÎÒÃÇ¿ÉÒÔÖ±½ÓÓÃÀ´×îÏÈÉè¼ÆÎÒÃǵÄÓ¦ÓóÌÐò·ÓÉ¡£ÀýÈ磬web.php ÎļþÖаüÀ¨Ò»¸öʾÀý·ÓÉ£º
Route::get('/', function () { return view('welcome'); });
µÇ¼ºó¸´ÖÆ
Õâ¸ö·Óɽç˵ÁËÒ»¸ö GET ÇëÇ󣬵± URL Ϊ “/” µÄʱ¼ä£¬·µ»ØÒ»¸öÃûΪ “welcome” µÄÊÓͼ¡£Í¬Ñù£¬api.php ÎļþÖÐÒ²ÌṩÁËʾÀý·ÓɵĽç˵¡£ÀýÈ磺
Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); });
µÇ¼ºó¸´ÖÆ
Õâ¸ö·Óɽç˵ÁËÒ»¸ö GET ÇëÇ󣬵± URL Ϊ “/user” µÄʱ¼ä£¬ÐèҪʹÓà “auth:api” ÖÐÐļþÑéÖ¤Óû§£¬²¢ÇÒ·µ»ØÄ¿½ñÓû§µÄÐÅÏ¢¡£
ÔÚ Laravel µÄ·ÓÉÉèÖÃÎļþÖУ¬ÎÒÃÇ¿ÉÒÔʹÓ÷Óɺê½ç˵À´¼ò»¯Â·Óɽç˵µÄ´úÂ롣·Óɺê½ç˵¿ÉÒÔÈÃÎÒÃǽ¨Éè¿ÉÖØÓõÄ·ÓÉÄ£°å£¬²¢ÔÚ²î±ðµÄ·ÓÉÖÐʹÓÃËüÃÇ¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚ web.php ÎļþÖнç˵һ¸öÃûΪ “dashboard” µÄºê½ç˵£º
Route::macro('dashboard', function () { Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard'); });
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇ¿ÉÒÔÔÚÓ¦ÓóÌÐòµÄÆäËûµØ·½Ê¹ÓÃÕâ¸öºê½ç˵£º
Route::middleware(['auth', 'verified'])->dashboard();
µÇ¼ºó¸´ÖÆ
Õâ¸ö·Óɽç˵ÁËÒ»¸ö GET ÇëÇ󣬵± URL Ϊ “/dashboard” ÇÒͨ¹ý “auth” ºÍ “verified” ÖÐÐļþÑéÖ¤µÄʱ¼ä£¬Å²Óà DashboardController ÀàÖÐµÄ index ÒªÁ죬²¢½«ÆäÃüÃûΪ “dashboard”¡£
Laravel µÄ·ÓÉÉèÖÃÎļþ»¹ÌṩÁË·ÓÉ·Ö×éµÄ¹¦Ð§¡£Â·ÓÉ·Ö×é¿ÉÒÔÈÃÎÒÃÇÔÚÒ»¸ö·ÓÉ×éÄÚΪһ×é·Óɽç˵ÏàͬµÄÖÐÐļþ¡¢ÃüÃûǰ׺ºÍÃüÃû¿Õ¼äµÈÑ¡Ïî¡£ÀýÈ磬ÔÚ web.php ÎļþÖУ¬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öÃûΪ “admin” µÄ·ÓÉ·Ö×飺
Route::prefix('admin')->middleware(['auth'])->group(function () { Route::get('/', [AdminController::class, 'dashboard'])->name('admin.dashboard'); Route::get('/users', [UserController::class, 'index'])->name('admin.users.index'); Route::get('/users/{id}', [UserController::class, 'show'])->name('admin.users.show'); });
µÇ¼ºó¸´ÖÆ
Õâ¸ö·ÓÉ·Ö×é½ç˵ÁËÒ»¸ö·ÓÉ·¾¶Ç°×º “admin”£¬ÐèҪʹÓà “auth” ÖÐÐļþÑéÖ¤Óû§£¬È»ºó½ç˵ÁËÈý¸ö×Ó·ÓÉ¡£µ± URL Ϊ “/admin” µÄʱ¼ä£¬Å²Óà AdminController ÀàÖÐµÄ dashboard ÒªÁ죬ÃüÃûΪ “admin.dashboard”£»µ± URL Ϊ “/admin/users” µÄʱ¼ä£¬Å²Óà UserController ÀàÖÐµÄ index ÒªÁ죬ÃüÃûΪ “admin.users.index”£»µ± URL Ϊ “/admin/users/{id}” µÄʱ¼ä£¬Å²Óà UserController ÀàÖÐµÄ show ÒªÁ죬ÃüÃûΪ “admin.users.show”¡£
·ÓÉ·Ö×黹¿ÉÒÔǶÌ×ʹÓã¬ÒÔ±ã¸üϸÁ£¶ÈµØ×é֯·ÓÉ¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚÉÏÀýÖÐµÄ “admin” ·ÓÉ·Ö×éÖÐÔÙ½¨ÉèÒ»¸öÃûΪ “settings” µÄ×Ó·Ö×飺
Route::prefix('settings')->group(function () { Route::get('/', [SettingsController::class, 'index'])->name('admin.settings.index'); Route::post('/', [SettingsController::class, 'update'])->name('admin.settings.update'); });
µÇ¼ºó¸´ÖÆ
Õâ¸ö×Ó·Ö×é½ç˵ÁËÒ»¸ö·ÓÉ·¾¶Ç°×º “settings”£¬È»ºó½ç˵ÁËÁ½¸ö×Ó·ÓÉ£¬Ò»¸ö GET ÇëÇóºÍÒ»¸ö POST ÇëÇ󣬻®·ÖŲÓà SettingsController ÀàÖÐµÄ index ºÍ update ÒªÁ죬²¢½«ËüÃÇÃüÃûΪ “admin.settings.index” ºÍ “admin.settings.update”¡£
×ܽ᣺
Laravel µÄ·ÓÉÉèÖÃÎļþÊǽç˵·ÓÉµÄ PHP Îļþ£¬Í¨³£ÉúÑÄÔÚ /routes Ŀ¼Ï£¬ÎļþÃûΪ web.php »ò api.php£¬»®·Ö¶ÔÓ¦ Web Ó¦ÓóÌÐòºÍ API Ó¦ÓóÌÐòµÄ·Óɽç˵¡£
ÔÚ·ÓÉÎļþÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà Laravel ÌṩµÄ·Óɺê½ç˵À´¼ò»¯´úÂ룬²¢ÇÒ¿ÉÒÔʹÓ÷ÓÉ·Ö×éÀ´ÎªÒ»×é·Óɽç˵ÏàͬµÄÖÐÐļþ¡¢ÃüÃûǰ׺ºÍÃüÃû¿Õ¼äµÈÑ¡Ïî¡£
·ÓÉ·Ö×é¿ÉÒÔǶÌ×ʹÓã¬ÒÔ±ã¸üϸÁ£¶ÈµØ×é֯·ÓÉ¡£
ÒÔÉϾÍÊÇlaravel·ÓÉÉèÖÃÎļþÊÇʲôµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡