matt
matt

Reputation: 2352

Restful API with Zend 2

I'm trying to create a rest api for my Zend 2 website and I'm getting a bit confused with routing, some of the logic behind it and where to use controllers.

Let's say I want to get a user: http://example.com/api/user/peter

This will go to ApiController, look for the userAction function, there I can get the param 'peter' and return some details.

The question is do I differentiate methods (post, get, put...) within the userAction function? Or my ApiController should contain getAction() postAction()... functions?

If it's the latter how can I differentiate these urls:

http://example.com/api/user/peter
http://example.com/api/house
http://example.com/api/animal/cat

Upvotes: 1

Views: 1473

Answers (2)

Eduardo E.R.
Eduardo E.R.

Reputation: 133

its something that is often discussed. Both things that you are talking about are perfectly valid, but have differents implementations, pros and cons.

IMPLEMENTATION 1

Use HTTP methods (post,get, put...) to differentiate what thing do you want (create, read, update...) in your userAction function.

for example http://example.com/api/user/peter:

function userAction()
{
    $method = $this->getRequest()->getMethod();

    switch ($method) 
    {  
        case 'POST':
            // Create peter user
        break;
        case 'GET':
            // Read peter user
        break;
        case 'PUT':
            // Update peter user
        break;
        case 'DELETE':
            // Delete peter user
        break;
    }
}

IMPLEMENTATION 2

Use differents zend actions (postAction(), getAction()...) in your apiController to determinate if you want to create, read, update... a user. In this case you need to determinate in the Method's API the action you want to do.

I suggest to use "api" as MODULE and "user" as CONTROLLER, it will be more scalable. For example:

Get the users identified by a set of name

URL: http://example.com/api/user/peter
MODULE: api
CONTROLLER: user
ACTION: index
PARAM: peter

In this case it could be combinate with more get params, like page or pagesize:

URL: http://example.com/api/user/peter?page=1&pagesize=25
MODULE: api
CONTROLLER: user
ACTION: index
PARAM: peter,page,pagesize

Delete a given user

URL: http://example.com/api/user/peter/delete
MODULE: api
CONTROLLER: user
ACTION: delete
PARAM: peter

Edit a given user

URL: http://example.com/api/user/peter/edit
MODULE: api
CONTROLLER: user
ACTION: edit
PARAM: peter

We use delete or edit word in the URL, to determinate what action we want to do. This is necesary to define the correct route and differentiate the urls.


In conclusion I prefer to use implementation 2. API's methods are more understandable and clean, your code will not become more complex.

If you want to dig in other API implementations to know how they do it, I encourage you to look Stack Exchange API

Upvotes: 1

Maltronic
Maltronic

Reputation: 1802

API builders like Apigility really simplify the creation of good quality RESTful APIs, including intuitive URL structures. To directly answer your question though, you can access the HTTP method from the controller action using:

$this->getRequest()->getMethod()

Upvotes: 2

Related Questions