1
1
The Big Picture
2
2
===============
3
3
4
- Start using Symfony2 in 10 minutes! This chapter walks you through some of the
5
- most important concepts behind Symfony2. It explains how to get started
6
- quickly by showing you the structure of a simple project.
4
+ Start using Symfony2 in 10 minutes! This chapter will walk you through some
5
+ of the most important concepts behind Symfony2 and explain how you can get
6
+ started quickly by showing you a simple project in action .
7
7
8
8
If you've used a web framework before, you should feel right at home with
9
9
Symfony2. If not, welcome to a whole new way of developing web applications!
10
10
11
- .. tip:
11
+ .. tip ::
12
12
13
13
Want to learn why and when you need to use a framework? Read the "`Symfony
14
14
in 5 minutes `_" document.
@@ -19,7 +19,7 @@ Downloading Symfony2
19
19
First, check that you have installed and configured a Web server (such as
20
20
Apache) with PHP 5.3.2 or higher.
21
21
22
- Ready? Let's start by downloading the "`Symfony2 Standard Edition `_", a Symfony
22
+ Ready? Start by downloading the "`Symfony2 Standard Edition `_", a Symfony
23
23
:term: `distribution ` that is preconfigured for the most common use cases and
24
24
also contains some code that demonstrates how to use Symfony2 (get the archive
25
25
with the *vendors * included to get started even faster).
@@ -35,17 +35,30 @@ have a ``Symfony/`` directory that looks like this:
35
35
cache/
36
36
config/
37
37
logs/
38
+ Resources/
39
+ bin/
38
40
src/
39
41
Acme/
40
42
DemoBundle/
41
43
Controller/
42
44
Resources/
45
+ ...
43
46
vendor/
44
47
symfony/
45
48
doctrine/
46
49
...
47
50
web/
48
51
app.php
52
+ ...
53
+
54
+ .. note ::
55
+
56
+ If you downloaded the Standard Edition *without vendors *, simply run the
57
+ following command to download all of the vendor libraries:
58
+
59
+ .. code-block :: bash
60
+
61
+ ./bin/vendors install
49
62
50
63
Checking the Configuration
51
64
--------------------------
@@ -60,8 +73,8 @@ URL to see the diagnostics for your machine:
60
73
61
74
If there are any outstanding issues listed, correct them. You might also tweak
62
75
your configuration by following any given recommendations. When everything is
63
- fine, click on "Go to the Welcome page" to request your first "real" Symfony2
64
- webpage:
76
+ fine, click on "* Bypass configuration and go to the Welcome page* " to request
77
+ your first "real" Symfony2 webpage:
65
78
66
79
.. code-block :: text
67
80
@@ -70,13 +83,14 @@ webpage:
70
83
Symfony2 should welcome and congratulate you for your hard work so far!
71
84
72
85
.. image :: /images/quick_tour/welcome.jpg
86
+ :align: center
73
87
74
88
Understanding the Fundamentals
75
89
------------------------------
76
90
77
- One of the main goals of a framework is to ensure the `Separation of Concerns `_.
91
+ One of the main goals of a framework is to ensure `Separation of Concerns `_.
78
92
This keeps your code organized and allows your application to evolve easily
79
- over time by avoiding the mix of database calls, HTML tags, and business
93
+ over time by avoiding the mixing of database calls, HTML tags, and business
80
94
logic in the same script. To achieve this goal with Symfony, you'll first
81
95
need to learn a few fundamental concepts and terms.
82
96
@@ -86,14 +100,17 @@ need to learn a few fundamental concepts and terms.
86
100
in the same script? Read the ":doc: `/book/from_flat_php_to_symfony2 `"
87
101
chapter of the book.
88
102
89
- The distribution comes with some sample code that you will use to learn more
103
+ The distribution comes with some sample code that you can use to learn more
90
104
about the main Symfony2 concepts. Go to the following URL to be greeted by
91
105
Symfony2 (replace *Fabien * with your first name):
92
106
93
107
.. code-block :: text
94
108
95
109
http://localhost/Symfony/web/app_dev.php/demo/hello/Fabien
96
110
111
+ .. image :: /images/quick_tour/hello_fabien.png
112
+ :align: center
113
+
97
114
What's going on here? Let's dissect the URL:
98
115
99
116
* ``app_dev.php ``: This is a :term: `front controller `. It is the unique entry
@@ -104,19 +121,22 @@ What's going on here? Let's dissect the URL:
104
121
105
122
Your responsibility as a developer is to write the code that maps the user's
106
123
*request * (``/demo/hello/Fabien ``) to the *resource * associated with it
107
- (``Hello Fabien! ``).
124
+ (the ``Hello Fabien! `` HTML page ).
108
125
109
126
Routing
110
127
~~~~~~~
111
128
112
129
Symfony2 routes the request to the code that handles it by trying to match the
113
130
requested URL against some configured patterns. By default, these patterns
114
131
(called routes) are defined in the ``app/config/routing.yml `` configuration
115
- file:
132
+ file. When you're in the ``dev `` :ref: `environment<quick-tour-big-picture-environments> ` -
133
+ indicated by the app_**dev**.php front controller - the ``app/config/routing_dev.yml ``
134
+ configuration file is also loaded. In the Standard Edition, the routes to
135
+ these "demo" pages are placed in that file:
116
136
117
137
.. code-block :: yaml
118
138
119
- # app/config/routing .yml
139
+ # app/config/routing_dev .yml
120
140
_welcome :
121
141
pattern : /
122
142
defaults : { _controller: AcmeDemoBundle:Welcome:index }
@@ -126,9 +146,12 @@ file:
126
146
type : annotation
127
147
prefix : /demo
128
148
149
+ # ...
150
+
129
151
The first three lines (after the comment) define the code that is executed
130
- when the user requests the "``/ ``" resource (i.e. the welcome page). When
131
- requested, the ``AcmeDemoBundle:Welcome:index `` controller will be executed.
152
+ when the user requests the "``/ ``" resource (i.e. the welcome page you saw
153
+ earlier). When requested, the ``AcmeDemoBundle:Welcome:index `` controller
154
+ will be executed. In the next section, you'll learn exactly what that means.
132
155
133
156
.. tip ::
134
157
@@ -142,12 +165,12 @@ requested, the ``AcmeDemoBundle:Welcome:index`` controller will be executed.
142
165
Controllers
143
166
~~~~~~~~~~~
144
167
145
- A controller handles incoming * requests * and returns * responses * (often HTML
146
- code). Instead of using the PHP global variables and functions (like `` $_GET ``
147
- or ``header() ``) to manage these HTTP messages, Symfony uses objects:
148
- : class: `Symfony\\ Component\\ HttpFoundation\\ Request ` and
149
- :class: `Symfony\\ Component\\ HttpFoundation\\ Response `. The simplest possible
150
- controller creates the response by hand, based on the request::
168
+ A controller is a fancy name for a PHP function or method that handles incoming
169
+ * requests * and returns * responses * (often HTML code). Instead of using the
170
+ PHP global variables and functions (like `` $_GET `` or ``header() ``) to manage
171
+ these HTTP messages, Symfony uses objects: : class: `Symfony\\ Component\\ HttpFoundation\\ Request `
172
+ and :class: `Symfony\\ Component\\ HttpFoundation\\ Response `. The simplest possible
173
+ controller might create the response by hand, based on the request::
151
174
152
175
use Symfony\Component\HttpFoundation\Response;
153
176
@@ -157,10 +180,10 @@ controller creates the response by hand, based on the request::
157
180
158
181
.. note ::
159
182
160
- Don't be fooled by the simple concepts and the power that they hold. Read
161
- the ":doc: `/book/http_fundamentals `" chapter of the book to learn more
162
- about how Symfony2 embraces HTTP and why it makes things simpler and more
163
- powerful at the same time .
183
+ Symfony2 embraces the HTTP Specification, which are the rules that govern
184
+ all communication on the We. Read the ":doc: `/book/http_fundamentals `"
185
+ chapter of the book to learn more about this and the added power that
186
+ this brings .
164
187
165
188
Symfony2 chooses the controller based on the ``_controller `` value from the
166
189
routing configuration: ``AcmeDemoBundle:Welcome:index ``. This string is the
@@ -182,13 +205,13 @@ the ``Acme\DemoBundle\Controller\WelcomeController`` class::
182
205
183
206
.. tip ::
184
207
185
- You could have used
186
- ``Acme\DemoBundle\Controller\WelcomeController::indexAction `` for the
187
- ``_controller `` value but if you follow some simple conventions, the
188
- logical name is more concise and allows for more flexibility.
208
+ You could have used the full class and method name -
209
+ ``Acme\DemoBundle\Controller\WelcomeController::indexAction `` - for the
210
+ ``_controller `` value. But if you follow some simple conventions, the
211
+ logical name is shorter and allows for more flexibility.
189
212
190
- The controller class extends the built-in ``Controller `` class, which provides
191
- useful shortcut methods, like the
213
+ The `` WelcomeController `` class extends the built-in ``Controller `` class,
214
+ which provides useful shortcut methods, like the
192
215
:method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::render `
193
216
method that loads and renders a template
194
217
(``AcmeDemoBundle:Welcome:index.html.twig ``). The returned value is a Response
@@ -203,6 +226,11 @@ Response can be tweaked before it is sent to the browser::
203
226
return $response;
204
227
}
205
228
229
+ No matter how you do it, the end goal of your controller is always to return
230
+ the ``Response `` object that should be delivered back to the user. This ``Response ``
231
+ object can be populated with HTML code, represent a client redirect, or even
232
+ return the contents of a JPG image with a ``Content-Type `` header of ``image/jpg ``.
233
+
206
234
.. tip ::
207
235
208
236
Extending the ``Controller `` base class is optional. As a matter of fact,
@@ -212,21 +240,23 @@ Response can be tweaked before it is sent to the browser::
212
240
213
241
The template name, ``AcmeDemoBundle:Welcome:index.html.twig ``, is the template
214
242
*logical name * and it references the
215
- ``src/Acme/DemoBundle/Resources/views/Welcome/index.html.twig `` file. The
216
- bundles section below will explain why this is useful.
243
+ ``Resources/views/Welcome/index.html.twig `` file inside the ``AcmeDemoBundle ``
244
+ (located at ``src/Acme/DemoBundle ``). The bundles section below will explain
245
+ why this is useful.
217
246
218
- Now, take a look at the end of the routing configuration again:
247
+ Now, take a look at the routing configuration again and find the ``_demo ``
248
+ key:
219
249
220
250
.. code-block :: yaml
221
251
222
- # app/config/routing .yml
252
+ # app/config/routing_dev .yml
223
253
_demo :
224
254
resource : " @AcmeDemoBundle/Controller/DemoController.php"
225
255
type : annotation
226
256
prefix : /demo
227
257
228
- Symfony2 can read the routing information from different resources written in
229
- YAML, XML, PHP, or even embedded in PHP annotations. Here, the resource
258
+ Symfony2 can read/import the routing information from different files written
259
+ in YAML, XML, PHP, or even embedded in PHP annotations. Here, the file's
230
260
*logical name * is ``@AcmeDemoBundle/Controller/DemoController.php `` and refers
231
261
to the ``src/Acme/DemoBundle/Controller/DemoController.php `` file. In this
232
262
file, routes are defined as annotations on action methods::
@@ -260,13 +290,13 @@ you can see, its value can be retrieved through the ``$name`` method argument.
260
290
extensively in Symfony2 as a convenient way to configure the framework
261
291
behavior and keep the configuration next to the code.
262
292
263
- If you take a closer look at the action code, you can see that instead of
264
- rendering a template like before, it just returns an array of parameters. The
265
- ``@Template() `` annotation tells Symfony to render the template for you,
266
- passing in each variable of the array to the template. The name of the
267
- template that's rendered follows the name of the controller. So, in this
268
- example, the ``AcmeDemoBundle:Demo:hello.html.twig `` template is rendered
269
- (located at ``src/Acme/DemoBundle/Resources/views/Demo/hello.html.twig ``).
293
+ If you take a closer look at the controller code, you can see that instead of
294
+ rendering a template and returning a `` Response `` object like before, it
295
+ just returns an array of parameters. The ``@Template() `` annotation tells
296
+ Symfony to render the template for you, passing in each variable of the array
297
+ to the template. The name of the template that's rendered follows the name
298
+ of the controller. So, in this example, the ``AcmeDemoBundle:Demo:hello.html.twig ``
299
+ template is rendered (located at ``src/Acme/DemoBundle/Resources/views/Demo/hello.html.twig ``).
270
300
271
301
.. tip ::
272
302
@@ -307,14 +337,25 @@ blog, a forum, ...) and which can be easily shared with other developers. As
307
337
of now, we have manipulated one bundle, ``AcmeDemoBundle ``. You will learn
308
338
more about bundles in the last chapter of this tutorial.
309
339
340
+ .. _quick-tour-big-picture-environments :
341
+
310
342
Working with Environments
311
343
-------------------------
312
344
313
- Now that you have a better understanding of how Symfony2 works, have a closer
314
- look at the bottom of the page; you will notice a small bar with the Symfony2
315
- logo. This is called the "Web Debug Toolbar" and it is the developer's best
316
- friend. But this is only the tip of the iceberg; click on the weird hexadecimal
317
- number to reveal yet another very useful Symfony2 debugging tool: the profiler.
345
+ Now that you have a better understanding of how Symfony2 works, take a closer
346
+ look at the bottom of any Symfony2 rendered page. You should notice a small
347
+ bar with the Symfony2 logo. This is called the "Web Debug Toolbar" and it
348
+ is the developer's best friend.
349
+
350
+ .. image :: /images/quick_tour/web_debug_toolbar.png
351
+ :align: center
352
+
353
+ But what you see initially is only the tip of the iceberg; click on the weird
354
+ hexadecimal number to reveal yet another very useful Symfony2 debugging tool:
355
+ the profiler.
356
+
357
+ .. image :: /images/quick_tour/profiler.png
358
+ :align: center
318
359
319
360
Of course, you won't want to show these tools when you deploy your application
320
361
to production. That's why you will find another front controller in the
@@ -360,17 +401,18 @@ one:
360
401
toolbar : true
361
402
intercept_redirects : false
362
403
363
- The ``dev `` environment (defined in ``config_dev.yml ``) inherits from the
364
- global ``config.yml `` file and extends it by enabling the web debug toolbar.
404
+ The ``dev `` environment (which loads the ``config_dev.yml `` configuration file)
405
+ imports the global ``config.yml `` file and then modifies it by, in this example,
406
+ enabling the web debug toolbar.
365
407
366
408
Final Thoughts
367
409
--------------
368
410
369
411
Congratulations! You've had your first taste of Symfony2 code. That wasn't so
370
412
hard, was it? There's a lot more to explore, but you should already see how
371
413
Symfony2 makes it really easy to implement web sites better and faster. If you
372
- are eager to learn more about Symfony2, dive into the next section: "The
373
- View".
414
+ are eager to learn more about Symfony2, dive into the next section:
415
+ " :doc: ` The View<the_view> ` ".
374
416
375
417
.. _Symfony2 Standard Edition : http://symfony.com/download
376
418
.. _Symfony in 5 minutes : http://symfony.com/symfony-in-five-minutes
0 commit comments