PHP Restful API Example

Source: Internet
Author: User
Tags php example

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 ' 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.