Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
51ae0156
Commit
51ae0156
authored
Oct 30, 2014
by
东方孤思子(Paris·QianSen)
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
complete the translation of routing section and wait for final check.
parent
290784f3
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
4 deletions
+31
-4
runtime-routing.md
docs/guide-zh-CN/runtime-routing.md
+31
-4
No files found.
docs/guide-zh-CN/runtime-routing.md
View file @
51ae0156
...
...
@@ -2,12 +2,12 @@
=======
当
[
入口脚本
](
structure-entry-scripts.md
)
在调用
[
[yii\web\Application::run()|run()
]
]
方法时,它进行的第一个操作就是解析输入的请求,然后实例化对应的
[
控制器操作
](
structure-controllers.md
)
处理这个请求。该过程就被称为
**引导路由(routing)**
。(译
者
注:中文里既是动词也是名词)
方法时,它进行的第一个操作就是解析输入的请求,然后实例化对应的
[
控制器操作
](
structure-controllers.md
)
处理这个请求。该过程就被称为
**引导路由(routing)**
。(译注:中文里既是动词也是名词)
## 解析路由 <a name="resolving-route"></a>
引导路由第一步,是解析
输
入请求为一个路由。如
[
控制器(Controllers)
](
structure-controllers.md#routes
)
引导路由第一步,是解析
传
入请求为一个路由。如
[
控制器(Controllers)
](
structure-controllers.md#routes
)
所描述的那样,路由是一个用于定位控制器操作的地址。这个过程通过
`request`
应用组件的
[
[yii\web\Request::resolve()|resolve()
]
]
方法实现,该方法会调用
[
URL 管理器
](
runtime-url-handling.md
)
进行实质上的请求解析工作。
...
...
@@ -16,7 +16,7 @@ the [Controllers](structure-controllers.md#routes) section, is used to address a
This is done by
[
[yii\web\Request::resolve()|resolve()
]
] method of the
`request`
application component.
The method invokes the
[
URL manager
](
runtime-url-handling.md
)
to do the actual request parsing work.
默认情况下,
输
入请求会包含一个名为
`r`
的
`GET`
参数,它的值即被视为路由。但是如果启用
默认情况下,
传
入请求会包含一个名为
`r`
的
`GET`
参数,它的值即被视为路由。但是如果启用
[
[yii\web\UrlManager::enablePrettyUrl|pretty URL feature
]
],确定请求路由时则会进行更多处理。具体的细节请参考
[
URL 的解析与生成
](
runtime-url-handling.md
)
章节。
...
...
@@ -25,13 +25,17 @@ as the route. However, if the [[yii\web\UrlManager::enablePrettyUrl|pretty URL f
more work will be done to determine the requested route. For more details, please refer to
the
[
URL Parsing and Generation
](
runtime-url-handling.md
)
section.
若好死不死地路由最终无法被确定,那么
`request`
组件会抛出
[
[yii\web\NotFoundHttpException
]
] 异常(译
者
注:大名鼎鼎的 404)。
若好死不死地路由最终无法被确定,那么
`request`
组件会抛出
[
[yii\web\NotFoundHttpException
]
] 异常(译注:大名鼎鼎的 404)。
In case a route cannot be determined, the
`request`
component will throw a
[
[yii\web\NotFoundHttpException
]
].
### 默认路由 <a name="default-route"></a>
如果进来的请求并没有提供一个具体的路由,一般这种情况多为于对首页的请求,此时就会启用由
[
[yii\web\Application::defaultRoute
]
] 属性所指定的路由。该属性的默认值为
`site/index`
,指向
`site`
控制器的
`index`
操作。你可以像这样在应用配置中自定义该属性:
If an incoming request does not specify a route, which often happens to the request for homepages,
the route specified by
[
[yii\web\Application::defaultRoute
]
] will be used. The default value of this property
is
`site/index`
, which refers to the
`index`
action of the
`site`
controller. You may customize this property
...
...
@@ -47,6 +51,9 @@ return [
### `catchAll` 路由(全拦截路由) <a name="catchall-route"></a>
有时候,你会想要将你的 Web 应用临时置于维护模式,所有的请求下都会显示同一张信息页。有很多种方法都可以实现这一点。但是其中一个最简单快捷的方法的是在应用配置中设置
[
[yii\web\Application::catchAll
]
] 属性:
Sometimes, you may want to put your Web application in maintenance mode temporarily and display the same
informational page for all requests. There are many ways to accomplish this goal. But one of the simplest
ways is to configure the
[
[yii\web\Application::catchAll
]
] property like the following in the application configuration:
...
...
@@ -58,23 +65,41 @@ return [
];
```
`catchAll`
属性需要一个数组做参数,该数组的第一个元素为路由,剩下的元素会(以名值对的形式)指定绑定于该操作的各个参数。
The
`catchAll`
property should take an array whose first element specifies a route, and
the rest of the elements (name-value pairs) specify the parameters to be bound to the action.
当设置了
`catchAll`
属性时,他会替换掉所有从输入的请求中解析出来的路由。如果是上文的这种设置,用于处理所有传入请求的操作都会是相同的
`site/offline`
。
When the
`catchAll`
property is set, it will replace any route resolved from the incoming requests.
With the above configuration, the same
`site/offline`
action will be used to handle all incoming requests.
## 创建一个操作 <a name="creating-action"></a>
一旦请求路由被确定了,紧接着的步骤就是创建一个“操作(action)”对象响应该路由。
Once the requested route is determined, the next step is to create the action object corresponding to the route.
路由可以通过里面的斜杠分割成多个组成片段,举个栗子,
`site/index`
可以分解为
`site`
和
`index`
两部分。每个片段都是指向某一模块(Module),控制器(Controller)或操作(action)的一个 ID。
The route is broken down into multiple parts by the slashes in it. For example,
`site/index`
will be
broken into
`site`
and
`index`
. Each part is an ID which may refer to a module, a controller or an action.
从路由的首个片段开始,应用会经过以下流程依次创建模块(如果有),控制器,以及操作:
Starting from the first part in the route, the application conducts the following steps to create modules (if any),
the controller and the action:
1.
设置应用主体为当前模块。
2.
检查当前模块的
[
[yii\base\Module::controllerMap|controller map(控制器映射表)
]
] 是否包含当前 ID。如果是,会根据该表中的配置创建一个控制器对象,然后跳到步骤五执行该路由的后续片段。
3.
检查该 ID 是否指向当前模块中
[
[yii\base\Module::modules|modules
]
] 属性里的模块列表中的一个模块。如果是,会根据该模块表中的配置创建一个模块对象,然后会以新创建的模块为环境,跳回步骤二解析下一段路由。
4.
将该 ID 视为控制器 ID,并创建控制器对象。用下个步骤解析路由里剩下的片段。
5.
控制器会在他的
[
[yii\base\Controller::actions()|action map(操作映射表)
]
]里搜索当前 ID。如果找得到,它会根据该映射表中的配置创建一个操作对象;反之,控制器则会尝试创建一个与该 ID
相对应,由某个 action 方法所定义的行内操作(inline action)。
1.
Set the application as the current module.
2.
Check if the
[
[yii\base\Module::controllerMap|controller map
]
] of the current module contains the current ID.
If so, a controller object will be created according to the controller configuration found in the map,
...
...
@@ -88,5 +113,7 @@ the controller and the action:
it creates an action according to the configuration found in the map. Otherwise, the controller will
attempt to create an inline action which is defined by an action method corresponding to the current ID.
在上面的步骤里,如果有任何错误发生,都会抛出
[
[yii\web\NotFoundHttpException
]
](译注:就是404),标识出路由引导失败。
Among the above steps, if any error occurs, a
[
[yii\web\NotFoundHttpException
]
] will be thrown, indicating
failure of the routing.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment