Padrino app with REST API

戏子无情 提交于 2020-01-06 13:02:47

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/20764145/padrino-app-with-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!