Skip to content

Commit f2ab245

Browse files
committed
feature #4374 [WCM] Revamped the Quick Start tutorial (javiereguiluz)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #4374). Discussion ---------- [WCM] Revamped the Quick Start tutorial | Q | A | ------------- | --- | Doc fix? | yes | New docs? | yes | Applies to | master (2.6+) | Fixed tickets | #4360 This is a total revamp of the Quick Start tutorial. Following the recent trend in the Symfony world, everything is simpler now. The main changes are: * The tutorial requires PHP 5.4 because this way everything is simpler. By the way, PHP 5.3 is unsupported since August 2014 and it's extremely dangerous to keep using it. * We use the `AppBundle` and the other changes that will introduce the standard edition soon. There are some pending PR. * Some advanced features, such as HTTP caching, have been removed. This tutorial is aimed for the total newcomer and it doesn't make sense to confuse him/her with things not needed at first. There is one important thing missing: * The section about installing Symfony is pending of the new Symfony installer, specially for Windows users. And one last comment: * Someone, which I cannot remember, commented a few weeks ago that maybe we should get rid of the different versions of this tutorial. I agree that the tutorial aimed for newcomers should be unique to avoid confusions and it should always explain the latest and greatest Symfony features. I've created this PR against the master branch for that reason and I ask the doc managers (@weaverryan, @wouterj and @xabbuh) to think about removing all the previous versions of this tutorial. Commits ------- 811f6e8 Included a bunch of fixes suggested by the awesome Symfony doc reviewers 940924c Added a bunch of fixes suggested by @xabbuh 6105acb Fixed a RST formatting issue baf4b9b Revamped the Quick Start tutorial
2 parents eb0d8ac + 811f6e8 commit f2ab245

File tree

4 files changed

+580
-514
lines changed

4 files changed

+580
-514
lines changed

Diff for: quick_tour/the_architecture.rst

+59-91
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ Understanding the Directory Structure
1313
The directory structure of a Symfony :term:`application` is rather flexible,
1414
but the recommended structure is as follows:
1515

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;
1818
* ``vendor/``: the third-party dependencies;
19-
* ``web/``: the web root directory.
19+
* ``web/``: the web root directory.
2020

2121
The ``web/`` Directory
2222
~~~~~~~~~~~~~~~~~~~~~~
2323

2424
The web root directory is the home of all public and static files like images,
2525
stylesheets, and JavaScript files. It is also where each :term:`front controller`
26-
lives::
26+
lives, such as the production controller shown here::
2727

2828
// web/app.php
2929
require_once __DIR__.'/../app/bootstrap.php.cache';
@@ -33,7 +33,9 @@ lives::
3333

3434
$kernel = new AppKernel('prod', false);
3535
$kernel->loadClassCache();
36-
$kernel->handle(Request::createFromGlobals())->send();
36+
$request = Request::createFromGlobals();
37+
$response = $kernel->handle($request);
38+
$response->send();
3739

3840
The controller first bootstraps the application using a kernel class (``AppKernel``
3941
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.
5153
This class must implement two methods:
5254

5355
* ``registerBundles()`` must return an array of all bundles needed to run the
54-
application;
55-
56+
application, as explained in the next section;
5657
* ``registerContainerConfiguration()`` loads the application configuration
5758
(more on this later).
5859

@@ -73,12 +74,21 @@ A bundle is kind of like a plugin in other software. So why is it called a
7374
Symfony, from the core framework features to the code you write for your
7475
application.
7576

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+
7682
Bundles are first-class citizens in Symfony. This gives you the flexibility
7783
to use pre-built features packaged in third-party bundles or to distribute your
7884
own bundles. It makes it easy to pick and choose which features to enable in
7985
your application and optimize them the way you want. And at the end of the day,
8086
your application code is just as *important* as the core framework itself.
8187

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+
8292
Registering a Bundle
8393
~~~~~~~~~~~~~~~~~~~~
8494

@@ -98,10 +108,10 @@ a single ``Bundle`` class that describes it::
98108
new Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
99109
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
100110
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
111+
new AppBundle\AppBundle();
101112
);
102113

103114
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
104-
$bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
105115
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
106116
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
107117
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
@@ -110,23 +120,23 @@ a single ``Bundle`` class that describes it::
110120
return $bundles;
111121
}
112122

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.
117126

118127
Configuring a Bundle
119128
~~~~~~~~~~~~~~~~~~~~
120129

121130
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:
123132

124133
.. code-block:: yaml
125134
126135
# app/config/config.yml
127136
imports:
128137
- { resource: parameters.yml }
129138
- { resource: security.yml }
139+
- { resource: services.yml }
130140
131141
framework:
132142
#esi: ~
@@ -138,7 +148,7 @@ PHP. Have a look at the default Symfony configuration:
138148
form: true
139149
csrf_protection: true
140150
validation: { enable_annotations: true }
141-
templating: { engines: ['twig'] } #assets_version: SomeVersionScheme
151+
templating: { engines: ['twig'] }
142152
default_locale: "%locale%"
143153
trusted_proxies: ~
144154
session: ~
@@ -148,34 +158,6 @@ PHP. Have a look at the default Symfony configuration:
148158
debug: "%kernel.debug%"
149159
strict_variables: "%kernel.debug%"
150160
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-
179161
# Swift Mailer Configuration
180162
swiftmailer:
181163
transport: "%mailer_transport%"
@@ -184,9 +166,11 @@ PHP. Have a look at the default Symfony configuration:
184166
password: "%mailer_password%"
185167
spool: { type: memory }
186168
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.
190174

191175
Each :term:`environment` can override the default configuration by providing a
192176
specific configuration file. For example, the ``dev`` environment loads the
@@ -207,64 +191,42 @@ and then modifies it to add some debugging tools:
207191
toolbar: true
208192
intercept_redirects: false
209193
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+
# ...
222195
223196
Extending a Bundle
224197
~~~~~~~~~~~~~~~~~~
225198

226199
In addition to being a nice way to organize and configure your code, a bundle
227200
can extend another bundle. Bundle inheritance allows you to override any existing
228201
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.
231202

232203
Logical File Names
233204
..................
234205

235206
When you want to reference a file from a bundle, use this notation:
236207
``@BUNDLE_NAME/path/to/file``; Symfony will resolve ``@BUNDLE_NAME``
237208
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.
241212

242213
Logical Controller Names
243214
........................
244215

245-
For controllers, you need to reference method names using the format
216+
For controllers, you need to reference actions using the format
246217
``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.
258220

259221
Extending Bundles
260222
.................
261223

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
268230
can override almost any part of another bundle!
269231

270232
Do you understand now why Symfony is so flexible? Share your bundles between
@@ -276,22 +238,28 @@ Using Vendors
276238
-------------
277239

278240
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.
282245

283246
Understanding the Cache and Logs
284247
--------------------------------
285248

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
289252
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
295263
296264
When developing a web application, things can go wrong in many ways. The log
297265
files in the ``app/logs/`` directory tell you everything about the requests

0 commit comments

Comments
 (0)