hanami / router

Ruby/Rack HTTP router
http://hanamirb.org
MIT License
362 stars 92 forks source link

Using a variable in a namespace #152

Closed malin-as closed 6 years ago

malin-as commented 6 years ago

Hi,

In order to have some internationalisation support on a project, I decided to give a try to URLs of the form https://server/en/home where en is the chosen locale for the interface. I played a bit with namespace:

root to: 'root#detect_locale'

namespace ':locale' do
  get     '/',       to: 'root#index',   as: :root
  get     '/home',   to: 'home#index',   as: :home
end

This actually works, but I'm afraid it was not designed for that purpose (and might stop working in a future version), since it's impossible to set a filter on it, and since it breaks the path name generation of resources.

What are your thoughts on such a feature? I could give a try at a PR.

jodosha commented 6 years ago

@malin-as This looks as a great addition. Please give it a try.

AlfonsoUceda commented 6 years ago

@malin-as maybe we don't need to amend namespace, what I think is we can add a new word for that porpuse and the word comes to my mind is scope, what do you think?

IMO namespace is designed to other purpose so I prefer to add a new word like scope or other name.

malin-as commented 6 years ago

@AlfonsoUceda I don't see clearly what the difference would be between them, from the point of view of a Hanami user. The way I see it, namespace would become just a special case of scope, when there's no variable involved.

Either way, I'm having trouble with the internal plumbing, trying to figure out how hanami-router and http_router interact exactly, and what http_router is compiling for each route, and how to debug that. I'm afraid that's too complicated for me.

jodosha commented 6 years ago

@AlfonsoUceda Can you please expand what you mean and what are the differences between namespace and scope? Thanks!

AlfonsoUceda commented 6 years ago

Just if it makes easier the implementation, namespace will create a namespace in ruby and scope just to work with variables.

jodosha commented 6 years ago

@AlfonsoUceda but having #namespace to dynamically create a Ruby namespace is a breaking change. Am I missing something?

jodosha commented 6 years ago

Closing as stale conversation. Is someone wants to take care of this feature, please reopen this issue.