基于路径的多租户 RoR 应用程序的 URL 路由

发布于 2025-01-03 00:15:57 字数 1053 浏览 2 评论 0 原文

我正在实现一个多租户 RoR 应用程序。使用路径中的第一段而不是子域作为租户标识符来识别租户。我的理解是 getsatisfaction.com 实现了这种多租户路由。例如:

http://myapp.com/tenant1/resource 而不是 http://tenant1.myapp.com, http://tenant2.myapp.com

我希望实现以下路由行为

get the tenant part from myapp.com/segement1/resource
if [segment1] has an entry in our db as a tenant
    then set base_url as [http://myapp.com/segment1], and do the route lookup for /resource
else
    set base_url as [http://myapp.com/] and do the route lookup for /segment1/resource

为了说明这一点,

http://myapp.com/login will not match any tenant, hence will login to the site
http://myapp.com/org1/tasks will match a tenant named org1, get the 'tasks' of org1
http://myapp.com/tasks will not many any tenant, get the task of all orgs

我尝试阅读 RoR paths.rb、url 重写、apache,但无法找出最佳方案方法来做到这一点。有关如何实现这一点的任何指示?

I am implementing a multitenant RoR application. Tenants are identified using the first segment in the path as the tenant identifier instead of subdomains. My understanding is that getsatisfaction.com implements this kind of multitenant routng. Ex:

http://myapp.com/tenant1/resource instead of http://tenant1.myapp.com, http://tenant2.myapp.com

I am looking to implement the following routing behaviour

get the tenant part from myapp.com/segement1/resource
if [segment1] has an entry in our db as a tenant
    then set base_url as [http://myapp.com/segment1], and do the route lookup for /resource
else
    set base_url as [http://myapp.com/] and do the route lookup for /segment1/resource

To illustrate

http://myapp.com/login will not match any tenant, hence will login to the site
http://myapp.com/org1/tasks will match a tenant named org1, get the 'tasks' of org1
http://myapp.com/tasks will not many any tenant, get the task of all orgs

I tried reading up RoR routes.rb, url rewrite, apache but unable to figure out the best way to do this. Any pointers on how to implement this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守望孤独 2025-01-10 00:15:57

您可以尝试确定一些路由的范围:

resources :tasks

scope ':tenant' do
  root to: 'dashboard#index', as: 'dashboard'
  resources :tasks
end

在您的 TasksController 中,您将获得一个 param[:tenant] 变量,可用于查找租户。如果 param[:tenant] 为 nil,则可以返回所有任务。

You could try scoping some routes:

resources :tasks

scope ':tenant' do
  root to: 'dashboard#index', as: 'dashboard'
  resources :tasks
end

In your TasksController, you'll get a param[:tenant] variable that you can use to look up the tenant. If param[:tenant] is nil, you can just return all tasks.

毅然前行 2025-01-10 00:15:57

您可以通过将

http://myapp.com/org1/tasks

设置为最后一条路线来实现此目的。

放置 http://myapp.com/loginhttp://myapp.com/tasks 在 org1/tasks 路由之前。因此,只有当登录和任务路由不匹配时,Rails 的路由器才会寻找更通用的路由

You can accomplish this by making

http://myapp.com/org1/tasks

the last route.

Put the routes for http://myapp.com/login and http://myapp.com/tasks before the org1/tasks route. So only if the login and tasks route are not matched, Rails' router will look for more generic routes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文