问题描述
我查看了 rails 3 架构以了解调度请求的过程.整个过程非常简单.应用程序是一个机架应用程序,它最终将其调用消息委托给 ActionDispatch::Routing::RouteSet 的调用方法,该方法调度必要控制器的适当动作.它通过键action_dispatch.request.path_parameters"从存储在机架环境中的哈希中获取控制器和动作名称.
I was looking at the rails 3 architecture in order to understand the process of dispatching a request . The whole process is quite simple. Application is a rack application which finally delegates its call message to the call method of ActionDispatch::Routing::RouteSet which dispatches appropriate action of necessary controller. It takes controller and action names from the hash stored in rack env by the key "action_dispatch.request.path_parameters".
所以问题是:谁设置了这个哈希?谁来解析请求 uri 并确定操作和控制器名称?
So the question is: Who sets this hash? Who parses request uri and determines action and controller names?
我找不到此代码.我看到在路由配置期间 ActionDispatch::Routing::Mapper 对象用于构造有关已定义路由的信息并将其存储在 ActionDispatch::Routing::RouteSet 中.但是在请求期间如何使用此信息来查找适当的操作和控制器.机架是否也以某种方式参与其中?
I was not able to find this code. I see that during route configuration ActionDispatch::Routing::Mapper object is used to construct information about defined routes and stores it in ActionDispatch::Routing::RouteSet. But how this information is used during the request to find appropriate action and controller. Is rack also somehow involved here?
推荐答案
"action_dispatch.request.path_parameters" 存储为常量 ActionDispatch::Routing::RouteSet::PARAMETERS_KEY
(actionpack/lib/action_dispatch/routing/route_set.rb)
"action_dispatch.request.path_parameters" is stored as the constant ActionDispatch::Routing::RouteSet::PARAMETERS_KEY
(actionpack/lib/action_dispatch/routing/route_set.rb)
PARAMETERS_KEY
稍后在同一个文件中使用并传递给 ::Rack::Mount::RouteSet
PARAMETERS_KEY
is used later on in that same file and passed into the constructer for ::Rack::Mount::RouteSet
转到 ::Rack::Mount::RouteSet
源代码:https://github.com/josh/rack-mount/blob/master/lib/rack/mount/route_set.rb#L22 你可以看到存储了属性名称.
Going to ::Rack::Mount::RouteSet
source here: https://github.com/josh/rack-mount/blob/master/lib/rack/mount/route_set.rb#L22 you can see that attribute name is stored.
同一个文件,在 call(env
) 方法中,当然是将被调用的 Rack 接口,我们看到这一行( https://github.com/josh/rack-mount/blob/master/lib/rack/mount/route_set.rb#L147 ),其中您的 env["action_dispatch.request.path_parameters"]
属性实际设置,并最终返回到您正在调查的代码中.
Same file, down in the call(env
) method which is of course the Rack interface that will be called into, we see this line ( https://github.com/josh/rack-mount/blob/master/lib/rack/mount/route_set.rb#L147 ) where your env["action_dispatch.request.path_parameters"]
attribute is actually set, and eventually returned back into the code you were investigating.
希望这会有所帮助!
这篇关于Rails 3 请求调度周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!