@@ -13,17 +13,17 @@ Understanding the Directory Structure
13
13
The directory structure of a Symfony :term: `application ` is rather flexible,
14
14
but the recommended structure is as follows:
15
15
16
- * ``app/ ``: the application configuration;
17
- * ``src/ ``: the project's PHP code;
16
+ * ``app/ ``: the application configuration, templates and translations ;
17
+ * ``src/ ``: the project's PHP code;
18
18
* ``vendor/ ``: the third-party dependencies;
19
- * ``web/ ``: the web root directory.
19
+ * ``web/ ``: the web root directory.
20
20
21
21
The ``web/ `` Directory
22
22
~~~~~~~~~~~~~~~~~~~~~~
23
23
24
24
The web root directory is the home of all public and static files like images,
25
25
stylesheets, and JavaScript files. It is also where each :term: `front controller `
26
- lives::
26
+ lives, such as the production controller shown here ::
27
27
28
28
// web/app.php
29
29
require_once __DIR__.'/../app/bootstrap.php.cache';
@@ -33,7 +33,9 @@ lives::
33
33
34
34
$kernel = new AppKernel('prod', false);
35
35
$kernel->loadClassCache();
36
- $kernel->handle(Request::createFromGlobals())->send();
36
+ $request = Request::createFromGlobals();
37
+ $response = $kernel->handle($request);
38
+ $response->send();
37
39
38
40
The controller first bootstraps the application using a kernel class (``AppKernel ``
39
41
in this case). Then, it creates the ``Request `` object using the PHP's global
@@ -51,8 +53,7 @@ configuration and as such, it is stored in the ``app/`` directory.
51
53
This class must implement two methods:
52
54
53
55
* ``registerBundles() `` must return an array of all bundles needed to run the
54
- application;
55
-
56
+ application, as explained in the next section;
56
57
* ``registerContainerConfiguration() `` loads the application configuration
57
58
(more on this later).
58
59
@@ -73,12 +74,21 @@ A bundle is kind of like a plugin in other software. So why is it called a
73
74
Symfony, from the core framework features to the code you write for your
74
75
application.
75
76
77
+ All the code you write for your application is organized in bundles. In Symfony
78
+ speak, a bundle is a structured set of files (PHP files, stylesheets, JavaScripts,
79
+ images, ...) that implements a single feature (a blog, a forum, ...) and which
80
+ can be easily shared with other developers.
81
+
76
82
Bundles are first-class citizens in Symfony. This gives you the flexibility
77
83
to use pre-built features packaged in third-party bundles or to distribute your
78
84
own bundles. It makes it easy to pick and choose which features to enable in
79
85
your application and optimize them the way you want. And at the end of the day,
80
86
your application code is just as *important * as the core framework itself.
81
87
88
+ Symfony already includes an ``AppBundle `` that you may use to start developing
89
+ your application. Then, if you need to split the application into reusable
90
+ components, you can create your own bundles.
91
+
82
92
Registering a Bundle
83
93
~~~~~~~~~~~~~~~~~~~~
84
94
@@ -98,10 +108,10 @@ a single ``Bundle`` class that describes it::
98
108
new Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
99
109
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
100
110
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
111
+ new AppBundle\AppBundle();
101
112
);
102
113
103
114
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
104
- $bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
105
115
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
106
116
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
107
117
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
@@ -110,23 +120,23 @@ a single ``Bundle`` class that describes it::
110
120
return $bundles;
111
121
}
112
122
113
- In addition to the AcmeDemoBundle that was already talked about, notice
114
- that the kernel also enables other bundles such as the FrameworkBundle,
115
- DoctrineBundle, SwiftmailerBundle and AsseticBundle bundle. They are all part
116
- of the core framework.
123
+ In addition to the AppBundle that was already talked about, notice that the
124
+ kernel also enables other bundles that are part of Symfony, such as FrameworkBundle,
125
+ DoctrineBundle, SwiftmailerBundle and AsseticBundle.
117
126
118
127
Configuring a Bundle
119
128
~~~~~~~~~~~~~~~~~~~~
120
129
121
130
Each bundle can be customized via configuration files written in YAML, XML, or
122
- PHP. Have a look at the default Symfony configuration:
131
+ PHP. Have a look at this sample of the default Symfony configuration:
123
132
124
133
.. code-block :: yaml
125
134
126
135
# app/config/config.yml
127
136
imports :
128
137
- { resource: parameters.yml }
129
138
- { resource: security.yml }
139
+ - { resource: services.yml }
130
140
131
141
framework :
132
142
# esi: ~
@@ -138,7 +148,7 @@ PHP. Have a look at the default Symfony configuration:
138
148
form : true
139
149
csrf_protection : true
140
150
validation : { enable_annotations: true }
141
- templating : { engines: ['twig'] } # assets_version: SomeVersionScheme
151
+ templating : { engines: ['twig'] }
142
152
default_locale : " %locale%"
143
153
trusted_proxies : ~
144
154
session : ~
@@ -148,34 +158,6 @@ PHP. Have a look at the default Symfony configuration:
148
158
debug : " %kernel.debug%"
149
159
strict_variables : " %kernel.debug%"
150
160
151
- # Assetic Configuration
152
- assetic :
153
- debug : " %kernel.debug%"
154
- use_controller : false
155
- bundles : [ ]
156
- # java: /usr/bin/java
157
- filters :
158
- cssrewrite : ~
159
- # closure:
160
- # jar: "%kernel.root_dir%/Resources/java/compiler.jar"
161
- # yui_css:
162
- # jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"
163
-
164
- # Doctrine Configuration
165
- doctrine :
166
- dbal :
167
- driver : " %database_driver%"
168
- host : " %database_host%"
169
- port : " %database_port%"
170
- dbname : " %database_name%"
171
- user : " %database_user%"
172
- password : " %database_password%"
173
- charset : UTF8
174
-
175
- orm :
176
- auto_generate_proxy_classes : " %kernel.debug%"
177
- auto_mapping : true
178
-
179
161
# Swift Mailer Configuration
180
162
swiftmailer :
181
163
transport : " %mailer_transport%"
@@ -184,9 +166,11 @@ PHP. Have a look at the default Symfony configuration:
184
166
password : " %mailer_password%"
185
167
spool : { type: memory }
186
168
187
- Each first level entry like ``framework ``, ``twig `` or ``doctrine `` defines the
188
- configuration for a specific bundle. For example, ``framework `` configures the
189
- FrameworkBundle while ``swiftmailer `` configures the SwiftmailerBundle.
169
+ # ...
170
+
171
+ Each first level entry like ``framework ``, ``twig `` and ``swiftmailer `` defines
172
+ the configuration for a specific bundle. For example, ``framework `` configures
173
+ the FrameworkBundle while ``swiftmailer `` configures the SwiftmailerBundle.
190
174
191
175
Each :term: `environment ` can override the default configuration by providing a
192
176
specific configuration file. For example, the ``dev `` environment loads the
@@ -207,64 +191,42 @@ and then modifies it to add some debugging tools:
207
191
toolbar : true
208
192
intercept_redirects : false
209
193
210
- monolog :
211
- handlers :
212
- main :
213
- type : stream
214
- path : " %kernel.logs_dir%/%kernel.environment%.log"
215
- level : debug
216
- firephp :
217
- type : firephp
218
- level : info
219
-
220
- assetic :
221
- use_controller : true
194
+ # ...
222
195
223
196
Extending a Bundle
224
197
~~~~~~~~~~~~~~~~~~
225
198
226
199
In addition to being a nice way to organize and configure your code, a bundle
227
200
can extend another bundle. Bundle inheritance allows you to override any existing
228
201
bundle in order to customize its controllers, templates, or any of its files.
229
- This is where the logical names (e.g. ``@AcmeDemoBundle/Controller/SecuredController.php ``)
230
- come in handy: they abstract where the resource is actually stored.
231
202
232
203
Logical File Names
233
204
..................
234
205
235
206
When you want to reference a file from a bundle, use this notation:
236
207
``@BUNDLE_NAME/path/to/file ``; Symfony will resolve ``@BUNDLE_NAME ``
237
208
to the real path to the bundle. For instance, the logical path
238
- ``@AcmeDemoBundle /Controller/DemoController .php `` would be converted to
239
- ``src/Acme/DemoBundle/ Controller/DemoController .php ``, because Symfony knows
240
- the location of the AcmeDemoBundle .
209
+ ``@AppBundle /Controller/DefaultController .php `` would be converted to
210
+ ``src/AppBundle/ Controller/DefaultController .php ``, because Symfony knows
211
+ the location of the AppBundle .
241
212
242
213
Logical Controller Names
243
214
........................
244
215
245
- For controllers, you need to reference method names using the format
216
+ For controllers, you need to reference actions using the format
246
217
``BUNDLE_NAME:CONTROLLER_NAME:ACTION_NAME ``. For instance,
247
- ``AcmeDemoBundle:Welcome:index `` maps to the ``indexAction `` method from the
248
- ``Acme\DemoBundle\Controller\WelcomeController `` class.
249
-
250
- Logical Template Names
251
- ......................
252
-
253
- For templates, the logical name ``AcmeDemoBundle:Welcome:index.html.twig `` is
254
- converted to the file path ``src/Acme/DemoBundle/Resources/views/Welcome/index.html.twig ``.
255
- Templates become even more interesting when you realize they don't need to be
256
- stored on the filesystem. You can easily store them in a database table for
257
- instance.
218
+ ``AppBundle:Default:index `` maps to the ``indexAction `` method from the
219
+ ``AppBundle\Controller\DefaultController `` class.
258
220
259
221
Extending Bundles
260
222
.................
261
223
262
- If you follow these conventions, then you can use :doc: `bundle inheritance</cookbook/bundles/inheritance> `
263
- to " override" files, controllers or templates. For example, you can create
264
- a bundle - AcmeNewBundle - and specify that it overrides AcmeDemoBundle .
265
- When Symfony loads the ``AcmeDemoBundle:Welcome :index `` controller, it will
266
- first look for the ``WelcomeController `` class in AcmeNewBundle and, if
267
- it doesn't exist, then look inside AcmeDemoBundle . This means that one bundle
224
+ If you follow these conventions, then you can use :doc: `bundle inheritance </cookbook/bundles/inheritance >`
225
+ to override files, controllers or templates. For example, you can create
226
+ a bundle - NewBundle - and specify that it overrides AppBundle .
227
+ When Symfony loads the ``AppBundle:Default :index `` controller, it will
228
+ first look for the ``DefaultController `` class in NewBundle and, if
229
+ it doesn't exist, then look inside AppBundle . This means that one bundle
268
230
can override almost any part of another bundle!
269
231
270
232
Do you understand now why Symfony is so flexible? Share your bundles between
@@ -276,22 +238,28 @@ Using Vendors
276
238
-------------
277
239
278
240
Odds are that your application will depend on third-party libraries. Those
279
- should be stored in the ``vendor/ `` directory. This directory already contains
280
- the Symfony libraries, the SwiftMailer library, the Doctrine ORM, the Twig
281
- templating system, and some other third party libraries and bundles.
241
+ should be stored in the ``vendor/ `` directory. You should never touch anything
242
+ in this directory, because it is exclusively managed by Composer. This directory
243
+ already contains the Symfony libraries, the SwiftMailer library, the Doctrine ORM,
244
+ the Twig templating system and some other third party libraries and bundles.
282
245
283
246
Understanding the Cache and Logs
284
247
--------------------------------
285
248
286
- Symfony is probably one of the fastest full-stack frameworks around. But how
287
- can it be so fast if it parses and interprets tens of YAML and XML files for
288
- each request? The speed is partly due to its cache system. The application
249
+ Symfony applications can contain tens of configuration files defined in several
250
+ formats (YAML, XML, PHP, etc.) Instead of parsing and combining all those files
251
+ for each request, Symfony uses its own cache system. In fact, the application
289
252
configuration is only parsed for the very first request and then compiled down
290
- to plain PHP code stored in the ``app/cache/ `` directory. In the development
291
- environment, Symfony is smart enough to flush the cache when you change a
292
- file. But in the production environment, to speed things up, it is your
293
- responsibility to clear the cache when you update your code or change its
294
- configuration.
253
+ to plain PHP code stored in the ``app/cache/ `` directory.
254
+
255
+ In the development environment, Symfony is smart enough to update the cache when
256
+ you change a file. But in the production environment, to speed things up, it is
257
+ your responsibility to clear the cache when you update your code or change its
258
+ configuration. Execute this command to clear the cache in the ``prod `` environment:
259
+
260
+ .. code-block :: bash
261
+
262
+ $ php app/console cache:clear --env=prod
295
263
296
264
When developing a web application, things can go wrong in many ways. The log
297
265
files in the ``app/logs/ `` directory tell you everything about the requests
0 commit comments