1
1
The Architecture
2
2
================
3
3
4
- You are my hero! Who would have thought that you would still be here after the
5
- first three parts? Your efforts will be well rewarded soon. The first three
6
- parts didn't look too deeply at the architecture of the framework. Because it
7
- makes Symfony stand apart from the framework crowd, let's dive into the
8
- architecture now.
4
+ You are my hero! Who would have thought that you would still be here after
5
+ the first three parts? Your efforts will be well rewarded soon. The first
6
+ three parts didn't look too deeply at the architecture of the framework.
7
+ Because it makes Symfony stand apart from the framework crowd, let's dive
8
+ into the architecture now.
9
9
10
10
Understanding the Directory Structure
11
11
-------------------------------------
@@ -26,7 +26,7 @@ The ``web/`` Directory
26
26
~~~~~~~~~~~~~~~~~~~~~~
27
27
28
28
The web root directory is the home of all public and static files like images,
29
- stylesheets, and JavaScript files. It is also where each :term: `front controller `
29
+ stylesheets and JavaScript files. It is also where each :term: `front controller `
30
30
lives, such as the production controller shown here::
31
31
32
32
// web/app.php
@@ -57,8 +57,8 @@ configuration and as such, it is stored in the ``app/`` directory.
57
57
This class must implement two methods:
58
58
59
59
``registerBundles() ``
60
- Must return an array of all bundles needed to run the application, as explained
61
- in the next section.
60
+ Must return an array of all bundles needed to run the application, as
61
+ explained in the next section.
62
62
``registerContainerConfiguration() ``
63
63
Loads the application configuration (more on this later).
64
64
@@ -74,24 +74,25 @@ Understanding the Bundle System
74
74
This section introduces one of the greatest and most powerful features of
75
75
Symfony, the :term: `bundle ` system.
76
76
77
- A bundle is kind of like a plugin in other software. So why is it called a
78
- *bundle * and not a *plugin *? This is because *everything * is a bundle in
79
- Symfony, from the core framework features to the code you write for your
80
- application.
77
+ A bundle is kind of like a plugin in other software. So why is it
78
+ called a *bundle * and not a *plugin *? This is because *everything * is a
79
+ bundle in Symfony, from the core framework features to the code you write
80
+ for your application.
81
81
82
- All the code you write for your application is organized in bundles. In Symfony
83
- speak, a bundle is a structured set of files (PHP files, stylesheets, JavaScripts ,
84
- images, ...) that implements a single feature (a blog, a forum, ...) and which
85
- can be easily shared with other developers.
82
+ All the code you write for your application is organized in bundles. In
83
+ Symfony speak, a bundle is a structured set of files (PHP files, stylesheets,
84
+ JavaScripts, images, ...) that implements a single feature (a blog, a forum,
85
+ ...) and which can be easily shared with other developers.
86
86
87
87
Bundles are first-class citizens in Symfony. This gives you the flexibility
88
- to use pre-built features packaged in third-party bundles or to distribute your
89
- own bundles. It makes it easy to pick and choose which features to enable in
90
- your application and optimize them the way you want. And at the end of the day,
91
- your application code is just as *important * as the core framework itself.
92
-
93
- Symfony already includes an AppBundle that you may use to start developing your
94
- application. Then, if you need to split the application into reusable
88
+ to use pre-built features packaged in third-party bundles or to distribute
89
+ your own bundles. It makes it easy to pick and choose which features to
90
+ enable in your application and optimize them the way you want. And at the
91
+ end of the day, your application code is just as *important * as the core
92
+ framework itself.
93
+
94
+ Symfony already includes an AppBundle that you may use to start developing
95
+ your application. Then, if you need to split the application into reusable
95
96
components, you can create your own bundles.
96
97
97
98
Registering a Bundle
@@ -125,15 +126,15 @@ a single Bundle class that describes it::
125
126
return $bundles;
126
127
}
127
128
128
- In addition to the AppBundle that was already talked about, notice that the
129
- kernel also enables other bundles that are part of Symfony, such as FrameworkBundle,
130
- DoctrineBundle, SwiftmailerBundle and AsseticBundle.
129
+ In addition to the AppBundle that was already talked about, notice that
130
+ the kernel also enables other bundles that are part of Symfony, such as
131
+ FrameworkBundle, DoctrineBundle, SwiftmailerBundle and AsseticBundle.
131
132
132
133
Configuring a Bundle
133
134
~~~~~~~~~~~~~~~~~~~~
134
135
135
- Each bundle can be customized via configuration files written in YAML, XML, or
136
- PHP. Have a look at this sample of the default Symfony configuration:
136
+ Each bundle can be customized via configuration files written in YAML, XML,
137
+ or PHP. Have a look at this sample of the default Symfony configuration:
137
138
138
139
.. code-block :: yaml
139
140
@@ -173,14 +174,15 @@ PHP. Have a look at this sample of the default Symfony configuration:
173
174
174
175
# ...
175
176
176
- Each first level entry like ``framework ``, ``twig `` and ``swiftmailer `` defines
177
- the configuration for a specific bundle. For example, ``framework `` configures
178
- the FrameworkBundle while ``swiftmailer `` configures the SwiftmailerBundle.
177
+ Each first level entry like ``framework ``, ``twig `` and ``swiftmailer ``
178
+ defines the configuration for a specific bundle. For example, ``framework ``
179
+ configures the FrameworkBundle while ``swiftmailer `` configures the
180
+ SwiftmailerBundle.
179
181
180
- Each :term: `environment ` can override the default configuration by providing a
181
- specific configuration file. For example, the ``dev `` environment loads the
182
- ``config_dev.yml `` file, which loads the main configuration (i.e. `` config.yml ``)
183
- and then modifies it to add some debugging tools:
182
+ Each :term: `environment ` can override the default configuration by providing
183
+ a specific configuration file. For example, the ``dev `` environment loads
184
+ the ``config_dev.yml `` file, which loads the main configuration (i.e.
185
+ `` config.yml ``) and then modifies it to add some debugging tools:
184
186
185
187
.. code-block :: yaml
186
188
@@ -202,8 +204,9 @@ Extending a Bundle
202
204
~~~~~~~~~~~~~~~~~~
203
205
204
206
In addition to being a nice way to organize and configure your code, a bundle
205
- can extend another bundle. Bundle inheritance allows you to override any existing
206
- bundle in order to customize its controllers, templates, or any of its files.
207
+ can extend another bundle. Bundle inheritance allows you to override any
208
+ existing bundle in order to customize its controllers, templates, or any
209
+ of its files.
207
210
208
211
Logical File Names
209
212
..................
@@ -226,13 +229,14 @@ For controllers, you need to reference actions using the format
226
229
Extending Bundles
227
230
.................
228
231
229
- If you follow these conventions, then you can use :doc: `bundle inheritance </cookbook/bundles/inheritance >`
230
- to override files, controllers or templates. For example, you can create
231
- a bundle - NewBundle - and specify that it overrides AppBundle.
232
- When Symfony loads the ``AppBundle:Default:index `` controller, it will
233
- first look for the ``DefaultController `` class in NewBundle and, if
234
- it doesn't exist, then look inside AppBundle. This means that one bundle
235
- can override almost any part of another bundle!
232
+ If you follow these conventions, then you can use
233
+ :doc: `bundle inheritance </cookbook/bundles/inheritance >` to override files,
234
+ controllers or templates. For example, you can create a bundle - NewBundle
235
+ - and specify that it overrides AppBundle. When Symfony loads the
236
+ ``AppBundle:Default:index `` controller, it will first look for the
237
+ ``DefaultController `` class in NewBundle and, if it doesn't exist, then
238
+ look inside AppBundle. This means that one bundle can override almost any
239
+ part of another bundle!
236
240
237
241
Do you understand now why Symfony is so flexible? Share your bundles between
238
242
applications, store them locally or globally, your choice.
@@ -245,37 +249,40 @@ Using Vendors
245
249
Odds are that your application will depend on third-party libraries. Those
246
250
should be stored in the ``vendor/ `` directory. You should never touch anything
247
251
in this directory, because it is exclusively managed by Composer. This directory
248
- already contains the Symfony libraries, the SwiftMailer library, the Doctrine ORM,
249
- the Twig templating system and some other third party libraries and bundles.
252
+ already contains the Symfony libraries, the SwiftMailer library, the Doctrine
253
+ ORM, the Twig templating system and some other third party libraries and
254
+ bundles.
250
255
251
256
Understanding the Cache and Logs
252
257
--------------------------------
253
258
254
- Symfony applications can contain several configuration files defined in several
255
- formats (YAML, XML, PHP, etc.) Instead of parsing and combining all those files
256
- for each request, Symfony uses its own cache system. In fact, the application
257
- configuration is only parsed for the very first request and then compiled down
258
- to plain PHP code stored in the ``app/cache/ `` directory.
259
+ Symfony applications can contain several configuration files defined in
260
+ several formats (YAML, XML, PHP, etc.) Instead of parsing and combining
261
+ all those files for each request, Symfony uses its own cache system. In
262
+ fact, the application configuration is only parsed for the very first request
263
+ and then compiled down to plain PHP code stored in the ``app/cache/ ``
264
+ directory.
259
265
260
- In the development environment, Symfony is smart enough to update the cache when
261
- you change a file. But in the production environment, to speed things up, it is
262
- your responsibility to clear the cache when you update your code or change its
263
- configuration. Execute this command to clear the cache in the ``prod `` environment:
266
+ In the development environment, Symfony is smart enough to update the cache
267
+ when you change a file. But in the production environment, to speed things
268
+ up, it is your responsibility to clear the cache when you update your code
269
+ or change its configuration. Execute this command to clear the cache in
270
+ the ``prod `` environment:
264
271
265
272
.. code-block :: bash
266
273
267
274
$ php app/console cache:clear --env=prod
268
275
269
- When developing a web application, things can go wrong in many ways. The log
270
- files in the ``app/logs/ `` directory tell you everything about the requests
276
+ When developing a web application, things can go wrong in many ways. The
277
+ log files in the ``app/logs/ `` directory tell you everything about the requests
271
278
and help you fix the problem quickly.
272
279
273
280
Using the Command Line Interface
274
281
--------------------------------
275
282
276
283
Each application comes with a command line interface tool (``app/console ``)
277
- that helps you maintain your application. It provides commands that boost your
278
- productivity by automating tedious and repetitive tasks.
284
+ that helps you maintain your application. It provides commands that boost
285
+ your productivity by automating tedious and repetitive tasks.
279
286
280
287
Run it without any arguments to learn more about its capabilities:
281
288
@@ -299,7 +306,7 @@ around as you see fit.
299
306
300
307
And that's all for the quick tour. From testing to sending emails, you still
301
308
need to learn a lot to become a Symfony master. Ready to dig into these
302
- topics now? Look no further - go to the official :doc: `/book/index ` and pick
303
- any topic you want.
309
+ topics now? Look no further - go to the official :doc: `/book/index ` and
310
+ pick any topic you want.
304
311
305
312
.. _Composer : http://getcomposer.org
0 commit comments