简体   繁体   English

Github中的友好URL

[英]Friendly URLs in Github

How has Github managed to get friendly URLs for representing repos of users? Github如何设法获得用于表示用户回购协议的友好URL? For a project called abc by username foo , how do they work around with a URL like: http://github.com/foo/abc . 对于使用用户名foo命名为abc的项目,它们如何使用类似http://github.com/foo/abc的URL进行工作。 Are they fetching the abc model for the DB from the title in the URL (which sounds unreasonable as they are modifying the titles). 他们是否从URL的标题中获取数据库的abc模型(这在他们修改标题时听起来是不合理的)。 How are they transferring the unique ID of the abc repo which they can fetch and show in the view? 他们如何传输可在视图中获取并显示的abc仓库的唯一ID?

The reason I ask is that I am facing a similar problem of creating friendlier URLs to view a resource. 我问的原因是我面临着创建友好的URL来查看资源的类似问题。 MongoDB's object IDs are quite long and make the URL look horrific. MongoDB的对象ID很长,使URL看起来很恐怖。 Is there a workaround? 有解决方法吗? All the tutorials that demonstrate CRUD (or REST) URLs for a resource always include the object's unique ID(eg http://mysite.org/post/1 or http://mysite.org/post/1/edit . Is there a better way to do it? 所有演示资源的CRUD(或REST)URL的教程都始终包含对象的唯一ID(例如, http://mysite.org/post/1 : http://mysite.org/post/1http://mysite.org/post/1/edit 。更好的方法呢?

Not having seen their code, I couldn't tell you exactly how they do it, but if you're using Rails there are at least two Ruby gems that will give you similar results: 没有看过他们的代码,我无法确切告诉您他们是如何做到的,但是如果您使用的是Rails,至少有两个Ruby gem可以为您提供类似的结果:

Take a look at Slugged and friendly_id 看看Sluggedfriendly_id

http://github.com/foo/abc is a unique repository identifier (for that repo's master branch). http://github.com/foo/abc是唯一的存储库标识符(用于该存储库的master分支)。 I'd assume that somewhere they have a table that looks like: 我假设他们在某个地方有一张桌子,看起来像:

repository-id | user-id | project-id

and are just looking up based on user and project rather than repository-id. 并且只是根据用户和项目而不是根据存储库ID进行查找。

You'd need to do some domain-specific mapping between internal and user-friendly ids, but you'd need to make sure that was a 1:1 mapping. 您需要在内部ID和用户友好ID之间进行一些特定于域的映射,但是您需要确保这是1:1映射。

See this rails cast on methods, gems and solutions to common problems you might get while modifying the application to use friendly urls. 请参阅此方法栏,了解将应用程序修改为使用友好的url时可能遇到的常见问题的方法,技巧和解决方案。

http://railscasts.com/episodes/314-pretty-urls-with-friendlyid?view=asciicast http://railscasts.com/episodes/314-pretty-urls-with-friendlyid?view=asciicast

(although Ryan Bates deserves the rep+ for this) (尽管Ryan Bates为此值得推荐)

I mocked a structure like this using FriendlyID and Nested Resources . 我使用FriendlyID和Nested Resources模拟了这样的结构。

Essentially, use friendly ID to get the to_param -ish slugs in your routes, then set up nested resources. 本质上,使用友好的ID来获取路由中的to_param插件,然后设置嵌套资源。 Using GitHub as an example: 以GitHub为例:

routes.rb 的routes.rb

resources :users do
  resources :repositories
end

Then in your controller, say, for repositories, you can check the existence of params[:user_id] and use that to determine the user from the route. 然后,在您的控制器中,例如对于存储库,您可以检查params[:user_id]的存在并使用它来确定路由中的用户。 The reason I check for existence is because I did something like (roughly): 我检查存在的原因是因为我做了(大致)这样的事情:

/myrepositories/:repository_id
/:user_id/:repository_id

So my controller does: 所以我的控制器会:

def show
  @user = params[:user_id] ? User.find(params[:user_id]) : current_user
end

I followed this tutorial here to get started with this same project. 在这里遵循了本教程以开始使用同一项目。

This is called URL rewriting if the web server does it (such as Apache), and routing when it happens in a web application framework (such as Ruby on Rails). 如果Web服务器(例如Apache)执行此操作,则将其称为URL重写;当Web应用程序框架(例如Ruby on Rails)中发生此操作时,将其称为路由

http://www.sinatrarb.com/intro#Routes http://www.sinatrarb.com/intro#Routes
http://httpd.apache.org/docs/current/mod/mod_rewrite.html http://httpd.apache.org/docs/current/mod/mod_rewrite.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM