laravelǰ̨½Ó¿ÚÔõôд
laravelÊÇÄ¿½ñºÜÊÇÊ¢ÐеÄphp¿ª·¢¿ò¼Ü£¬Ö§³Ö¿ìËÙ¿ª·¢ºÍЧÀÍÆ÷¶ËÓ¦ÓóÌÐò¹¹½¨¡£ÔÚÏÖʵÏîÄ¿ÖУ¬¿ª·¢Õßͨ³£ÐèÒª±àдǰ̨½Ó¿Ú£¬ÒÔʵÏÖÇ°¶ËºÍºó¶ËµÄÊý¾Ý½»»¥ºÍͨѶ¡£ÏÂÃ潫ÏÈÈÝÔõÑùʹÓÃlaravel¿ò¼Ü±àдǰ̨½Ó¿Ú¡£
Ò»¡¢×°ÖÃLaravel
Ê×ÏÈÐèÒªÔÚÍâµØÇéÐÎ×°ÖÃLaravel¿ò¼Ü¡£¿ÉÒÔÑ¡ÔñʹÓÃComposer¾ÙÐÐ×°Öã¬Ö´ÐÐÏÂÁ
composer create-project –prefer-dist laravel/laravel projectName
ÕâÑù»áÔÚÄ¿½ñĿ¼Ï½¨ÉèÒ»¸öÃûΪprojectNameµÄLaravelÏîÄ¿¡£LaravelÖ§³ÖPHP 7.1»ò¸ü¸ß°æ±¾¡£
¶þ¡¢±àд·ÓÉ
LaravelÖУ¬Â·Óɽç˵ÁËÇëÇóURLÓë´¦Àí³ÌÐòÖ®¼äµÄ¹Øϵ¡£ÎÒÃÇÐèÒªÔÚroutes/web.phpÎļþÖнç˵·ÓÉ¡£
Route::get(‘/api/users’, ‘UserController@index’);
ÉÏÃæµÄ´úÂë½ç˵ÁË/api/usersÕâ¸öÇëÇóURL»á±»Ö¸¶¨µÄ´¦Àí³ÌÐòUserController@index´¦Àí¡£ÕâÀïµÄindexÊÇUserControllerÀàÖеĴ¦ÀíÒªÁ죬ÓÃÓÚ»ñÈ¡Óû§ÁÐ±í¡£
Èý¡¢±àд¿ØÖÆÆ÷
ÔÚÉÏÒ»²½ÖÐÎÒÃǽç˵ÁËUserControllerÀ࣬½ÓÏÂÀ´ÐèÒªÔÚapp/Http/ControllersĿ¼Ï½¨ÉèUserController.phpÎļþ£¬²¢ÔÚ¸ÃÎļþÖÐʵÏÖindexÒªÁì¡£
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppUser;
class UserController extends Controller
{
public function index() { $users = User::all(); return response()->json($users); }
µÇ¼ºó¸´ÖÆ
}
ÉÏÃæµÄ´úÂëÖÐÎÒÃÇʹÓÃÁËLaravelÄÚÖõÄÏìÓ¦ÖúÊÖº¯Êýresponse()£¬ÓÃÓÚ½«Ð§¹ûת»»ÎªJSONÃûÌò¢·µ»Øµ½Ç°Ì¨¡£
ËÄ¡¢±àдģ×Ó
ÔÚÉÏÒ»²½ÖÐÎÒÃÇʹÓÃÁËUserÄ£×ÓÀ´»ñÈ¡Óû§ÁÐ±í¡£Òò´ËÎÒÃÇ»¹ÐèÒªÔÚappĿ¼Ï½¨ÉèÒ»¸öUser.phpÄ£×ÓÎļþ£¬²¢ÔÚÆäÖнç˵ģ×Ó¡£
namespace App;
use IlluminateDatabaseEloquentModel;
class User extends Model
{
protected $fillable = [ 'name', 'email', 'password', ];
µÇ¼ºó¸´ÖÆ
}
ÉÏÃæµÄ´úÂëÖÐÎÒÃǽç˵ÁËUserÄ£×ÓµÄfillableÊôÐÔ£¬ÓÃÓÚÖ¸¶¨¿ÉÒÔ±»Ìî³äµÄ×ֶΡ£
Îå¡¢²âÊÔ½Ó¿Ú
ÏÖÔÚÎÒÃÇÒѾ½ç˵ºÃÁË·ÓÉ¡¢¿ØÖÆÆ÷ºÍÄ£×Ó£¬¿ÉÒÔʵÑéʹÓÃRESTful APIÀ´²âÊÔÎÒÃǵĽӿڡ£
Æô¶¯LaravelЧÀÍÆ÷
ÔÚÏîÄ¿¸ùĿ¼Ï£¬Ö´ÐÐÒÔÏÂÏÂÁîÆô¶¯LaravelЧÀÍÆ÷£º
php artisan serve
Õ⽫Æô¶¯Ò»¸öWebЧÀÍÆ÷£¬¼àÌý8000¶Ë¿Ú¡£
·¢ËÍHTTPÇëÇó
ʹÓÃPostmanµÈ¹¤¾ß£¬·¢ËÍÇëÇó http://localhost:8000/api/users£¬¼´¿É»ñÈ¡ËùÓÐÓû§µÄJSONÃûÌÃÊý¾Ý¡£
Áù¡¢×ܽá
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃÇÒѾÀֳɵØʹÓÃLaravel¿ò¼Ü±àдÁËÒ»¸ö¼òÆÓµÄRESTful API½Ó¿Ú¡£ËäÈ»£¬ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇ»¹ÐèҪ˼Á¿ÆäËûµÄÒòËØ£¬Èç²ÎÊýУÑé¡¢Çå¾²ÐÔ¡¢»º´æµÈ·½Ãæ¡£¿ÉÊÇÕâÀïÖ»ÊÇÒ»¸ö¼òÆÓµÄʵÀý£¬¿ÉÒÔ×ÊÖúÎÒÃÇ¿ìËÙÉÏÊÖʹÓÃLaravel¿ò¼Ü¿ª·¢Ç°Ì¨½Ó¿Ú¡£
ÒÔÉϾÍÊÇlaravelǰ̨½Ó¿ÚÔõôдµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡