Creating APIs with CakePHP

I have a simple CakePHP application that allows the user to create and edit messages. And I want to get the application in PhoneGap at some point in the future.

Therefore, I created an API that spits out JSON for use in AJAX requests, but I get the feeling that I am doing it wrong, because I do not use REST or do nothing that distinguishes it from other code in the controller.

eg. (NOTE: I am missing a part to turn it into JSON for this example)

class ApiController extends AppController {

    function index() {
        $posts= $this->Post->find('all');
        $this->set(compact('posts'));
    }
}

To create a URL, for example: domain.com/api/posts/all(will create its own route for this), which I can then use with AJAX to use in my mobile application.

Now my question is, what else would it do with REST? I am very new to creating applications, and my strengths are in the interface, and not in the reverse development, so any pointers, help with this will be highly appreciated.

+5
source share
2 answers

Including REST in CakePHP basically directs the appropriate HTTP methods to action. This way the GET request will be redirected to the index or view action, the DELETE request redirected to the delete action, etc.

This creates a very easy endpoint for people using your API. Then, when calling this endpoint, depending on the HTTP method, Cake will direct it to the correct action (skip any syntax errors of the HTTP request):

// single endpoint
http://example.com/api/posts

GET, /posts/index.json

GET /api/posts.json HTTP/1.1
Host: example.com

POST, /posts/edit/ 1.json

POST /api/posts/1.json HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Content-Length: 24

data[Post][name]=updated

: http://book.cakephp.org/2.0/en/development/rest.html

+3

.

, , 4 :

  • URI -
  • - , -.
    JSON, XML YAML, .
  • , - HTTP (, GET, PUT, POST DELETE).
  • API

. http://en.wikipedia.org/wiki/Representational_state_transfer.

, , , .

1) , , URI ( - , ):

api.domain.com/resources/posts = > URI

2) , / HTTP, , , :

api.domain.com/resources/posts/12

, URI:

: application/json
Content-Type: application/json
Request Url: http://api.domain.com/resources/posts/12
: GET

URI, (1),

, URI, :

domain.com/api/posts/

URI :

resources/posts/12 // ,
/ / .

:

. .

abstract class ResourcesController extends AppController {
}


class PostResourcesController extends ResourcesController {

    /**
     * By the time this method is called in your controller/class, you already know
     * that the HTTP method is GET.
     * 
     * @param Request\$_GET $request A request instance
     * @param int           $postId  The post ID to retrieve
     *
     * @return Response A reponse instance
     */
    function getPost(Request $Request, $postId = null)
    {
      /**
       * Here you can use the request object to get
       * the response content type    
       * the requesting client accepts. Example JSON or XML.
       */

       /**
        * using the $postId you can then query your database  
        * to retrieve a post with that ID or use a sort of 
        * service.
        */

         /**
         * Once you implemented a you logic
         * you can build a response to return.
         */
    }
}

, , , Restful API.

, , " , : ".

, .

+3

All Articles