This article to share the content is the PHP Restful API example, has a certain reference value, the need for friends can refer to
Therefore, there must be a unified mechanism to facilitate the communication of different front-end devices to the backend. This led to the popularity of API architecture, and even the "API first" design idea. RESTful API is one of the more mature API design theories for Internet applications
The RESTful API format is as follows:
Get/zoos: List all zoos Post/zoos: Create a new Zoo GET/ZOOS/ID: Get information about a designated Zoo PUT/ZOOS/ID: update information for a given zoo (all information about the zoo) patch/zoos/ ID: Update information for a specified zoo (providing some information about the zoo) delete/zoos/id: Delete a zoo get/zoos/id/animals: List all animals in a designated zoo delete/zoos/id/animals/ ID: Delete a designated animal from a designated zoo
The PHP example is as follows:
Routing files:
Get list $router->get (' Activity ', ' activitycontroller@index '); Get an activity $router->get (' Activity/{id} ', ' Activitycontroller@get '); Create an Activity $router->post (' Activity ', ' activitycontroller@create '); Update an activity $router->put (' Activity/{id} ', ' activitycontroller@update '); Delete an activity $router->delete (' Activity/{id} ', ' Activitycontroller@delete ');
The controller file is as follows:
Use Restfulapi to implement activity Curdclass activitycontroller{ //Get list public function index () { return Resp::outs (Activity::all ()); } Gets an active public function Get ($id) { return resp::outs (activity::where (' id ', $id)->first ()); } Create an active public function Create (Request $request) { $data = $request->all (); Return Resp::outs (Activity::create ($data)); } Update an active public function update (Request $request, $id) { $data = $request->all (); Return resp::outs (activity::where (' id ', $id)->update ($data)); } Delete an active public function Delete ($id) { return resp::outs (activity::where (' id ', $id)->delete ())} }
Can be tested with curl
Curl ' http://www.loanapi.com/activity ' curl ' http://www.loanapi.com/activity/26 ' curl-x POST ' http://www.loanapi.com /activity '-d ' title=xly ' curl-x PUT ' http://www.loanapi.com/activity/26 '-d ' title=xly ' curl-x DELETE ' Http://www.loana Pi.com/activity/26 '
Therefore, there must be a unified mechanism to facilitate the communication of different front-end devices to the backend. This led to the popularity of API architecture, and even the "API first" design idea. RESTful API is one of the more mature API design theories for Internet applications
The RESTful API format is as follows:
Get/zoos: List all zoos Post/zoos: Create a new Zoo GET/ZOOS/ID: Get information about a designated Zoo PUT/ZOOS/ID: update information for a given zoo (all information about the zoo) patch/zoos/ ID: Update information for a specified zoo (providing some information about the zoo) delete/zoos/id: Delete a zoo get/zoos/id/animals: List all animals in a designated zoo delete/zoos/id/animals/ ID: Delete a designated animal from a designated zoo
The PHP example is as follows:
Routing files:
Get list $router->get (' Activity ', ' activitycontroller@index '); Get an activity $router->get (' Activity/{id} ', ' Activitycontroller@get '); Create an Activity $router->post (' Activity ', ' activitycontroller@create '); Update an activity $router->put (' Activity/{id} ', ' activitycontroller@update '); Delete an activity $router->delete (' Activity/{id} ', ' Activitycontroller@delete ');
The controller file is as follows:
Use Restfulapi to implement activity Curdclass activitycontroller{ //Get list public function index () { return Resp::outs (Activity::all ()); } Gets an active public function Get ($id) { return resp::outs (activity::where (' id ', $id)->first ()); } Create an active public function Create (Request $request) { $data = $request->all (); Return Resp::outs (Activity::create ($data)); } Update an active public function update (Request $request, $id) { $data = $request->all (); Return resp::outs (activity::where (' id ', $id)->update ($data)); } Delete an active public function Delete ($id) { return resp::outs (activity::where (' id ', $id)->delete ())} }
Can be tested with curl
Curl ' http://www.loanapi.com/activity ' curl ' http://www.loanapi.com/ Activity/26 ' curl-x POST ' http://www.loanapi.com/activity '-d ' title=xly ' curl-x PUT ' http://www.loanapi.com/activity/ + '-d ' title=xly ' curl-x DELETE ' HTTP://WWW.LOANAPI.COM/ACTIVITY/26 '