Jump to content
Larry Ullman's Book Forums

Organization For Rest


Jonathon
 Share

Recommended Posts

Hi Larry

 

How would I organise my folders for a rest application. It's more so that I'll have an existing website and then want to make an app version of it or provide some functionality through an app.

 

So I'm taking that I would need to create rest controllers to do the work that my websites controllers currently do? But then I wasn't really sure what else?

 

Is it better to just build the app controllers as its own website so to speak on a different domain?

Link to comment
Share on other sites

It's quite normal to build the REST api alongside your site. It seems api.domain.com or domain.com/api/ are pretty normalt structures. While that is the most common, I myself prefer the sort of structure Rails use. This has the added benefit of allowing you to build everything once only. I don't know how easy this is to do with YII, but that's how I build in Symfony. Looking quickly at YII, the same should be possible there.

 

Some of the reason why this is so easy are some incredible bundles like the FOSRestBundle and JMSSerilizerBundle. By defining core configuration for you app, you're controllers can be configured by annotations only.

 

Here's an example of that incredible power:

/**
 * Class MatchController
 * @package JCN\Bundle\ApiBundle\Controller
 *
 * @Route("/match", name="api-match")
 */
class MatchController extends FOSRestController
{
    ...

    /**
     * @Route("/next", name="api-match-next")
     * @View(templateVar="match")
     * @Template
     */
    public function nextAction()
    {
        $em = $this->getDoctrine()->getManager();
        $next = array_pop($em->getRepository('JCNApiBundle:Fixture')->nextMatch());

        return array("match" => $next);
    }

    ...
}

Try the following Curl calls in the command line:

curl -i -H "Accept: application/json" http://jcn.juvenorge.com/api/match/next

curl -i -H "Accept: application/html" http://jcn.juvenorge.com/api/match/next

xmllint --format | curl -i -H "Accept: application/xml" http://jcn.juvenorge.com/api/match/next

 

To explain some of the magic here, those annotations gets parsed. When that is done, real objects are created behind the. scene, and does all the magic of handling format parsing, view rendering and routing. I can't really explain how much I love Symfony. :)

 

Edit: I want to add that this structure is not without it's drawback. If you publish your API and want other people using it, this kind of structure is terrible. Then it's more maintanable to offer api.domain.com/v1/action so BC are not broken every time you change something. This API will be for internal use in my own application.

Link to comment
Share on other sites

Hi Thomas

 

I was going to ask you about the annotations. That is pretty impressive. I have only just started with REST as a subject and I am of course doing it in my trusty Yii2. http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html

 

I decided in the end to build on a separate domain, mainly to keep everything separate and start small and incremental in REST terms. Although I do see the drawbacks of having to duplicate at least aspects of the actual domain. There isn't a huge amount to my app really, more so pulling data that is easily provided naturally in Yii and a few other custom functions.

Link to comment
Share on other sites

Sorry for the delay, Jonathan! I'd definitely do the API on a subdomain of the same URL or on another URL. It is a duplication of effort, yes, but it allows you to develop the API as a separate product. You can then make decisions about putting different functionality in your API than you have in your main site, or vice versa. 

Link to comment
Share on other sites

 Share

×
×
  • Create New...