Skip to content

Commit

Permalink
applications,routing,tags,index
Browse files Browse the repository at this point in the history
  • Loading branch information
netstu committed Dec 18, 2012
1 parent 23db9f1 commit 27ee98f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 102 deletions.
2 changes: 1 addition & 1 deletion zh/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Phalcon不只是性能优越,我们的目标是让它强大而且易于使用!

本文档的中文地址存放在 http://phalcon.5iunix.net

Github上的地址为: https://github.com/netstu/phalcondocs ,您如果发现有些地方译的有些操蛋,烦请你clone它,并完善她。
Github上的地址为: https://github.com/netstu/phalcondocs ,您如果发现有些地方译的有些操蛋,烦请你fork它,并完善她。

目录
-------------
Expand Down
49 changes: 18 additions & 31 deletions zh/reference/applications.rst
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
MVC Applications
================
All the hard work behind orchestrating the operation of MVC in Phalcon is normally done by
:doc:`Phalcon\\Mvc\\Application <../api/Phalcon_Mvc_Application>`. This component encapsulates all the complex
operations required in the background, instantiating every component needed and integrating it with the
project, to allow the MVC pattern to operate as desired.
在 Phalcon 中,所有复杂的MVC相关工作都是由 :doc:`Phalcon\\Mvc\\Application <../api/Phalcon_Mvc_Application>` 来完成的。该组件封装了所有复杂的后台操作,包括每一个组件的实例化,组件之间的集成等。

Single or Multi Module Applications
-----------------------------------
With this component you can run various types of MVC structures:
使用此组件,您可以运行不同类型的MVC结构:

Single Module
^^^^^^^^^^^^^
Single MVC applications consist of one module only. Namespaces can be used but are not necessary.
An application like this would have the following file structure:
单MVC应用程序只包含一个module,可以使用命名空间,但不是必需的。这样的应用程序的文件结构如下:

.. code-block:: php
Expand All @@ -26,7 +22,7 @@ An application like this would have the following file structure:
img/
js/
If namespaces are not used, the following bootstrap file could be used to orchestrate the MVC flow:
如果不使用命名空间,引导文件被用来协调MVC流程:

.. code-block:: php
Expand Down Expand Up @@ -58,7 +54,7 @@ If namespaces are not used, the following bootstrap file could be used to orches
echo $e->getMessage();
}
If namespaces are used, the following bootstrap can be used:
如果使用命名空间,引导文件可以这样做:

.. code-block:: php
Expand Down Expand Up @@ -103,7 +99,7 @@ If namespaces are used, the following bootstrap can be used:
Multi Module
^^^^^^^^^^^^
A multi-module application uses the same document root for more than one module. In this case the following file structure can be used:
一个multi-module(多模块)的应用程序是指使用相同的Document Root,但有超过一个module。在这种情况下,程序的文件结构如下:

.. code-block:: php
Expand All @@ -124,7 +120,7 @@ A multi-module application uses the same document root for more than one module.
img/
js/
Each directory in apps/ have its own MVC structure. A Module.php is present to configure specific settings of each module like autoloaders or custom services:
apps/ 目录下的每个目录都有自己的MVC结构,Module.php是每个Module特定的设置:

.. code-block:: php
Expand Down Expand Up @@ -178,7 +174,7 @@ Each directory in apps/ have its own MVC structure. A Module.php is present to c
}
A special bootstrap file is required to load the a multi-module MVC architecture:
一个特殊的引导文件,用以载入 multi-module MVC 结构:

.. code-block:: php
Expand Down Expand Up @@ -250,8 +246,7 @@ A special bootstrap file is required to load the a multi-module MVC architecture
echo $e->getMessage();
}
If you want to maintain the module configuration in the bootstrap file you can use an anonymous function to register the
module:
如果你想把配置文件完全写入到引导文件,你可以使用一个匿名函数的方式来注册 Module :

.. code-block:: php
Expand All @@ -278,16 +273,11 @@ module:
)
);
When :doc:`Phalcon\\Mvc\\Application <../api/Phalcon_Mvc_Application>` have modules registered, always is
necessary that every matched route returns a valid module. Each registered module has an associated class
that offers functions to set the module itself up. Each module class definition must implement two
methods: registerAutoloaders() and registerServices(), they will be called by
:doc:`Phalcon\\Mvc\\Application <../api/Phalcon_Mvc_Application>` according to the module to be executed.
:doc:`Phalcon\\Mvc\\Application <../api/Phalcon_Mvc_Application>` Module注册后,每个匹配的route都必须返回一个有效的module。注册的module都有一个相关的类,用于设置module本身提供的功能。每个module类都必须实现 registerAutoloaders() 和 registerServices() 这两个方法,:doc:`Phalcon\\Mvc\\Application <../api/Phalcon_Mvc_Application>` 将调用它们执行要执行的module。

Understanding the default behavior
了解默认行为
----------------------------------
If you've been following the :doc:`tutorial <tutorial>` or have generated the code using :doc:`Phalcon Devtools <tools>`,
you may recognize the following bootstrap file:
如果你一直关注 :doc:`tutorial <tutorial>` 或 使用 :doc:`Phalcon Devtools <tools>` 生成过代码,你可能会熟悉以下的引导文件:

.. code-block:: php
Expand All @@ -310,15 +300,15 @@ you may recognize the following bootstrap file:
echo "PhalconException: ", $e->getMessage();
}
The core of all the work of the controller occurs when handle() is invoked:
所有控制器工作的核心是 handle()方法被调用:

.. code-block:: php
<?php
echo $application->handle()->getContent();
If you do not wish to use :doc:`Phalcon\\Mvc\\Application <../api/Phalcon_Mvc_Application>`, the code above can be changed as follows:
如果您不希望使用 :doc:`Phalcon\\Mvc\\Application <../api/Phalcon_Mvc_Application>` ,上面的代码可以修改如下:

.. code-block:: php
Expand Down Expand Up @@ -364,14 +354,11 @@ If you do not wish to use :doc:`Phalcon\\Mvc\\Application <../api/Phalcon_Mvc_Ap
// Print the response
echo $response->getContent();
Although the above is a lot more verbose than the code needed while using :doc:`Phalcon\\Mvc\\Application <../api/Phalcon_Mvc_Application>`,
it offers an alternative in boostraping your application. Depending on your needs, you might want to have full control of what
should be instantiated or not, or replace certain components with those of your own to extend the default functionality.
尽管上面的代码显得比使用 :doc:`Phalcon\\Mvc\\Application <../api/Phalcon_Mvc_Application>` 罗唆,但它提供了一种替代bootstrap文件的方式。根据你的需要,你可能希望完全掌握哪些类应该被实例化,或使用自己的组件来扩展默认的功能。

Application Events
------------------
:doc:`Phalcon\\Mvc\\Application <../api/Phalcon_Mvc_Application>` is able to send events to the :doc:`EventsManager <events>`
(if it is present). Events are triggered using the type "application". The following events are supported:
:doc:`Phalcon\\Mvc\\Application <../api/Phalcon_Mvc_Application>` 能够将事件发送到 :doc:`EventsManager <events>`,事件管理器通过触发 "application"来实现,支持以下的事件:

+---------------------+--------------------------------------------------------------+
| Event Name | Triggered |
Expand All @@ -385,7 +372,7 @@ Application Events
| afterHandleRequest | After execute the dispatch loop |
+---------------------+--------------------------------------------------------------+

The following example demonstrates how to attach listeners to this component:
下面的示例演示如何在此组件上添加监听器:

.. code-block:: php
Expand All @@ -402,7 +389,7 @@ The following example demonstrates how to attach listeners to this component:
}
);
External Resources
相关资源
------------------

* `MVC examples on Github <https://github.com/phalcon/mvc>`_
38 changes: 11 additions & 27 deletions zh/reference/routing.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
Routing
=======
The router component allows defining routes that are mapped to controllers or handlers that should receive
the request. A router simply parses a URI to determine this information. The router has two modes: MVC
mode and match-only mode. The first mode is ideal for working with MVC applications.
路由器(Routing)
=============================
router组件允许定义用户请求对应到哪个控制器或Action。router解析 URI 以确定这些信息。路由器有两种模式:MVC模式和匹配模式(match-only)。第一种模式是使用MVC应用程序的理想选择。

Defining Routes
---------------
:doc:`Phalcon\\Mvc\\Router <../api/Phalcon_Mvc_Router>` provides advanced routing capabilities. In MVC mode,
you can define routes and map them to controllers/actions that you require. A route is defined as follows:
:doc:`Phalcon\\Mvc\\Router <../api/Phalcon_Mvc_Router>` 提供了一套先进的路由功能。在MVC模式中,你可以自定义路由规则,对应到你需要的 controllers/actions 上。路由的定义如下:

.. code-block:: php
Expand Down Expand Up @@ -36,14 +33,9 @@ you can define routes and map them to controllers/actions that you require. A ro
$router->handle();
The method add() receives as first parameter a pattern and optionally a set of paths as second parameter.
In this case if the URI is exactly: /admin/users/my-profile, then the "users" controller with its action "profile"
will be executed. Currently, the router does not execute the controller and action, it only collects this
information to inform the correct component (ie. :doc:`Phalcon\\Mvc\\Dispatcher <../api/Phalcon_Mvc_Dispatcher>`)
that this is controller/action it should to execute.
add() 方法接收两个参数,第一个参数是一个匹配字符串,第二个参数为一组可选的路径。在这种情况下,URI /admin/users/my-profile, "users"代表控制器,"profile"代表Ation。目前,该路由器不并不执行控制器和Action,只为组件(如: :doc:`Phalcon\\Mvc\\Dispatcher <../api/Phalcon_Mvc_Dispatcher>`) 收集信息,然后由分发器决定是否立即执行。

An application can have many paths, define routes one by one can be a cumbersome task. In these cases we can
create more flexible routes:
应用程序可能有很多个不同的路径,如果一个一个的定义路由的话,会非常麻烦。在这种情况下,我们可以使用更灵活的方式创建route:

.. code-block:: php
Expand All @@ -62,8 +54,7 @@ create more flexible routes:
)
);
In the example above, using wildcards we make a route valid for many URIs. For example, by accessing the
following URL (/admin/users/a/delete/dave/301) then:
在上面的例子中,我们使用通配符来匹配路由。例如,通过访问URL (/admin/users/a/delete/dave/301) ,解析为:

+------------+---------------+
| Controller | users |
Expand All @@ -75,17 +66,11 @@ following URL (/admin/users/a/delete/dave/301) then:
| Parameter | 301 |
+------------+---------------+

The method add() receives a pattern that optionally could have predefined placeholders and regular expression
modifiers. All the routing patterns must start with a slash character (/). The regular expression syntax used
is the same as the `PCRE regular expressions`_. Note that, it is not necessary to add regular expression
delimiters. All routes patterns are case-insensitive.
add()方法接收一个模式,可选择使用预定义占位符和正则表达式修饰符。所有的路由模式必须以斜线字符(/)开始。正则表达式语法使用与 `PCRE regular expressions`_ 相同的语法。需要注意的是,不必要添加正则表达式分隔符。所有的路由模式是不区分大小写的。

The second parameter defines how the matched parts should bind to the controller/action/parameters. Matching
parts are placeholders or subpatterns delimited by parentheses (round brackets). In the above example, the
first subpattern matched (:controller) is the controller part of the route, the second the action and so on.
第二个参数定义了如何将匹配部分绑定到controller/action/parameters。匹配部分是占位符或团圆括号中的子模式。另外,在上述的例子中,第一个子模式匹配(:controller),是route中控制器部分,第二个是action,等。

These placeholders help writing regular expressions that are more readable for developers and easier
to understand. The following placeholders are supported:
这些占位符使用正则表达式,更易读,更容易为开发人员理解。支持以下占位符:

+--------------+---------------------+--------------------------------------------------------------------+
| Placeholder | Regular Expression | Usage |
Expand All @@ -103,8 +88,7 @@ to understand. The following placeholders are supported:
| /:int | /([0-9]+) | Matches an integer parameter |
+--------------+---------------------+--------------------------------------------------------------------+

Controller names are camelized, this means that characters (-) and (_) are removed and the next character
is uppercased. For instance, some_controller is converted to SomeController.
控制器名称采用驼峰书写规则,这意味着,字符 (-) 和 (_)将被移除,同时把下一个字符转化为大写字符。比如,some_controller被转化为SomeController。

Since you can add many routes as you need using add(), the order in which you add the routes indicates
their relevance, last routes added have more relevance than first added. Internally, all defined routes
Expand Down
Loading

0 comments on commit 27ee98f

Please sign in to comment.