문제

Within a Padrino application I have a posts controller with the conventional routes:

Blog::App.controllers :posts do

  get :index do
    ...
  end

  get :show, :with => :id do
    ...
  end

end

This gives me therefore the normal URL access within the posts namespace

http://blog.dev/posts
http://blog.dev/posts/show/1

Now I want to provide access through a REST API from a different route outside the namespace, like for example:

http://blog.dev/api/v1/post/all
http://blog.dev/api/v1/post/1

How can I define the API for my posts controller with routes outside its normal namespace?

I must admit that I am not sure if my approach is the common one. I could define a new API V1 controller but in that case, I will have to duplicate information about posts in two different places.

In case this should be done with an API controller, I wonder which gems and/or conventions are commonly use for this task. Ideally I would like something that coordinates different API versions with the current model object.

도움이 되었습니까?

해결책

Blog::App.controllers :posts, map: '/api/v1/posts' do

  get :index do
    ...
  end

end

And then, if you want add new versions of that controller

Blog::App.controllers :v2_posts, map: '/api/v2/posts' do

  get :index do
    ...
  end

end

(Yeah, it seems you can't have several files with the same controller with different map values.)

So, this won't work (sorry if that works, that doesn't when I tried) correctly and will cause issues :

Blog::App.controllers :posts, map: '/api/v1/posts' do

  get :index do
    ...
  end

end

Blog::App.controllers :posts, map: '/api/v2/posts' do

  get :index do
    ...
  end

end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top