开发者

CakePHP Route for API

Basically i'm developing an API with cake and I'd like to put some version control in my URL to make upgrading the API easier later on.

I'm not looking for any fancy routes at the moment, simply that the 开发者_开发问答route

domain.com/api/1.9/:controller works.

I'm having a hard time getting this to work. At the moment simple requests work, but sending a POST request to domain.com/api/1.0/pictures/ fails.


Frank Mullenger has the best solution for a versioned CakePHP API that I have found. He's posted a three-part series that makes good use of Cake's routing and some custom API methods.

First part (the problem with un-versioned APIs)

Second part (the solution for CakePHP)

Third part (error handling)


Here's a thought about doing what you want without using clever routing, but you would need to change up the rest call URL a little.

instead of

domain.com/api/1.0/pictures/$param1/$param2/$etc

try

domain.com/api/pictures/1.0/$param1/$param2/$etc

then in the controller,

class Pictures extends controller {
  ....
  function pictures( $ver , $param1, $param2, $etc) {
    switch($ver) {
      case '1.3':
        $this->setAction('pictures_1-3', $param1, $param2, $etc);
        break;
      case '1.2':
        $this->setAction('pictures_1-2', $param1, $param2, $etc);
        break;
      case '1.1':
      default:
        $this->setAction('pictures_1-1', $param1, $param2, $etc);
        break;
     }
   }

   function pictures_1-3() {}
   function pictures_1-2() {}
   function pictures_1-1() {}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜