1
1
MVC Applications
2
2
================
3
- All the hard work behind orchestrating the operation of MVC in Phalcon is normally done by
4
- :doc: `Phalcon\\ Mvc\\ Application <../api/Phalcon_Mvc_Application >`. This component encapsulates all the complex
5
- operations required in the background, instantiating every component needed and integrating it with the
6
- project, to allow the MVC pattern to operate as desired.
3
+ 在 Phalcon 中,所有复杂的MVC相关工作都是由 :doc: `Phalcon\\ Mvc\\ Application <../api/Phalcon_Mvc_Application >` 来完成的。该组件封装了所有复杂的后台操作,包括每一个组件的实例化,组件之间的集成等。
7
4
8
5
Single or Multi Module Applications
9
6
-----------------------------------
10
- With this component you can run various types of MVC structures:
7
+ 使用此组件,您可以运行不同类型的MVC结构:
11
8
12
9
Single Module
13
10
^^^^^^^^^^^^^
14
- Single MVC applications consist of one module only. Namespaces can be used but are not necessary.
15
- An application like this would have the following file structure:
11
+ 单MVC应用程序只包含一个module,可以使用命名空间,但不是必需的。这样的应用程序的文件结构如下:
16
12
17
13
.. code-block :: php
18
14
@@ -26,7 +22,7 @@ An application like this would have the following file structure:
26
22
img/
27
23
js/
28
24
29
- If namespaces are not used, the following bootstrap file could be used to orchestrate the MVC flow:
25
+ 如果不使用命名空间,引导文件被用来协调MVC流程:
30
26
31
27
.. code-block :: php
32
28
@@ -58,7 +54,7 @@ If namespaces are not used, the following bootstrap file could be used to orches
58
54
echo $e->getMessage();
59
55
}
60
56
61
- If namespaces are used, the following bootstrap can be used:
57
+ 如果使用命名空间,引导文件可以这样做:
62
58
63
59
.. code-block :: php
64
60
@@ -103,7 +99,7 @@ If namespaces are used, the following bootstrap can be used:
103
99
104
100
Multi Module
105
101
^^^^^^^^^^^^
106
- A multi -module application uses the same document root for more than one module. In this case the following file structure can be used:
102
+ 一个multi -module(多模块)的应用程序是指使用相同的Document Root,但有超过一个module。在这种情况下,程序的文件结构如下:
107
103
108
104
.. code-block :: php
109
105
@@ -124,7 +120,7 @@ A multi-module application uses the same document root for more than one module.
124
120
img/
125
121
js/
126
122
127
- 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:
123
+ 在 apps/ 目录下的每个目录都有自己的MVC结构, Module.php是每个Module特定的设置:
128
124
129
125
.. code-block :: php
130
126
@@ -178,7 +174,7 @@ Each directory in apps/ have its own MVC structure. A Module.php is present to c
178
174
179
175
}
180
176
181
- A special bootstrap file is required to load the a multi-module MVC architecture:
177
+ 一个特殊的引导文件,用以载入 multi-module MVC 结构:
182
178
183
179
.. code-block :: php
184
180
@@ -250,8 +246,7 @@ A special bootstrap file is required to load the a multi-module MVC architecture
250
246
echo $e->getMessage();
251
247
}
252
248
253
- If you want to maintain the module configuration in the bootstrap file you can use an anonymous function to register the
254
- module:
249
+ 如果你想把配置文件完全写入到引导文件,你可以使用一个匿名函数的方式来注册 Module :
255
250
256
251
.. code-block :: php
257
252
@@ -278,16 +273,11 @@ module:
278
273
)
279
274
);
280
275
281
- When :doc: `Phalcon\\ Mvc\\ Application <../api/Phalcon_Mvc_Application >` have modules registered, always is
282
- necessary that every matched route returns a valid module. Each registered module has an associated class
283
- that offers functions to set the module itself up. Each module class definition must implement two
284
- methods: registerAutoloaders() and registerServices(), they will be called by
285
- :doc: `Phalcon\\ Mvc\\ Application <../api/Phalcon_Mvc_Application >` according to the module to be executed.
276
+ 当 :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。
286
277
287
- Understanding the default behavior
278
+ 了解默认行为
288
279
----------------------------------
289
- If you've been following the :doc: `tutorial <tutorial >` or have generated the code using :doc: `Phalcon Devtools <tools >`,
290
- you may recognize the following bootstrap file:
280
+ 如果你一直关注 :doc: `tutorial <tutorial >` 或 使用 :doc: `Phalcon Devtools <tools >` 生成过代码,你可能会熟悉以下的引导文件:
291
281
292
282
.. code-block :: php
293
283
@@ -310,15 +300,15 @@ you may recognize the following bootstrap file:
310
300
echo "PhalconException: ", $e->getMessage();
311
301
}
312
302
313
- The core of all the work of the controller occurs when handle() is invoked:
303
+ 所有控制器工作的核心是 handle()方法被调用:
314
304
315
305
.. code-block :: php
316
306
317
307
<?php
318
308
319
309
echo $application->handle()->getContent();
320
310
321
- If you do not wish to use :doc: `Phalcon\\ Mvc\\ Application <../api/Phalcon_Mvc_Application >`, the code above can be changed as follows:
311
+ 如果您不希望使用 :doc: `Phalcon\\ Mvc\\ Application <../api/Phalcon_Mvc_Application >` ,上面的代码可以修改如下:
322
312
323
313
.. code-block :: php
324
314
@@ -364,14 +354,11 @@ If you do not wish to use :doc:`Phalcon\\Mvc\\Application <../api/Phalcon_Mvc_Ap
364
354
// Print the response
365
355
echo $response->getContent();
366
356
367
- Although the above is a lot more verbose than the code needed while using :doc: `Phalcon\\ Mvc\\ Application <../api/Phalcon_Mvc_Application >`,
368
- it offers an alternative in boostraping your application. Depending on your needs, you might want to have full control of what
369
- should be instantiated or not, or replace certain components with those of your own to extend the default functionality.
357
+ 尽管上面的代码显得比使用 :doc: `Phalcon\\ Mvc\\ Application <../api/Phalcon_Mvc_Application >` 罗唆,但它提供了一种替代bootstrap文件的方式。根据你的需要,你可能希望完全掌握哪些类应该被实例化,或使用自己的组件来扩展默认的功能。
370
358
371
359
Application Events
372
360
------------------
373
- :doc: `Phalcon\\ Mvc\\ Application <../api/Phalcon_Mvc_Application >` is able to send events to the :doc: `EventsManager <events >`
374
- (if it is present). Events are triggered using the type "application". The following events are supported:
361
+ :doc: `Phalcon\\ Mvc\\ Application <../api/Phalcon_Mvc_Application >` 能够将事件发送到 :doc: `EventsManager <events >`,事件管理器通过触发 "application"来实现,支持以下的事件:
375
362
376
363
+---------------------+--------------------------------------------------------------+
377
364
| Event Name | Triggered |
@@ -385,7 +372,7 @@ Application Events
385
372
| afterHandleRequest | After execute the dispatch loop |
386
373
+---------------------+--------------------------------------------------------------+
387
374
388
- The following example demonstrates how to attach listeners to this component:
375
+ 下面的示例演示如何在此组件上添加监听器:
389
376
390
377
.. code-block :: php
391
378
@@ -402,7 +389,7 @@ The following example demonstrates how to attach listeners to this component:
402
389
}
403
390
);
404
391
405
- External Resources
392
+ 相关资源
406
393
------------------
407
394
408
395
* `MVC examples on Github <https://github.com/phalcon/mvc >`_
0 commit comments