Skip to content

Commit 3beccde

Browse files
committed
Comments thanks to Javier and fixing build issues
1 parent a1b02e9 commit 3beccde

File tree

5 files changed

+220
-27
lines changed

5 files changed

+220
-27
lines changed

index.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ Get started fast with the Symfony :doc:`Quick Tour <quick_tour/index>`:
1414
quick_tour/index
1515

1616
* :doc:`quick_tour/the_big_picture`
17-
* :doc:`quick_tour/the_view`
18-
* :doc:`quick_tour/the_controller`
17+
* :doc:`quick_tour/flex_recipes`
1918
* :doc:`quick_tour/the_architecture`
2019

2120
Getting Started

quick_tour/flex_recipes.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Flex: Compose your Application
44
After reading the first part of this tutorial, you have decided that Symfony was
55
worth another 10 minutes. Great choice! In this second part, you'll learn about
66
Symfony Flex: the amazing tool that makes adding new features as simple as running
7-
one command. It's also the reason why Symfony is perfect for a small micro-service
7+
one command. It's also the reason why Symfony is ideal for a small micro-service
88
or a huge application. Curious? Perfect!
99

1010
Symfony: Start Micro!
@@ -53,17 +53,16 @@ It's a way for a library to automatically configure itself by adding and modifyi
5353
files. Thanks to recipes, adding features is seamless and automated: install a package
5454
and you're done!
5555

56-
You an find a full list of recipes and aliases by going to `https://symfony.sh`_.
56+
You can find a full list of recipes and aliases by going to `https://symfony.sh`_.
5757

58-
What did this recipe do? In addition to automatically enabling the bundle in
58+
What did this recipe do? In addition to automatically enabling the feature in
5959
``config/bundles.php``, it added 3 things:
6060

6161
``config/packages/twig.yaml``
6262
A configuration file that sets up Twig with sensible defaults.
6363

6464
``config/routes/dev/twig.yaml``
65-
A route that helps you debug your error pages: it's added only in the ``dev``
66-
environment.
65+
A route that helps you debug your error pages.
6766

6867
``templates/``
6968
This is the directory where template files will live. The recipe also added
@@ -130,7 +129,7 @@ One of the *coolest* features of Symfony isn't even installed yet! Let's fix tha
130129
$ composer require profiler
131130
132131
Yes! This is another alias! And Flex *also* installs another recipe, which automates
133-
the configuration of Symfony's WebProfilerBundle. What's the result? Refresh!
132+
the configuration of Symfony's Profiler. What's the result? Refresh!
134133

135134
See that black bar on the bottom? That's the web debug toolbar, and it's your new
136135
best friend. By hovering over each icon, you can get information about what controller
@@ -237,8 +236,8 @@ Not convinced yet? No problem: remove the library:
237236
Flex will *uninstall* the recipes: removing files and un-doing changes to put your
238237
app back in its original state. Experiment without worry.
239238

240-
More Feature, Architecture and Speed
241-
------------------------------------
239+
More Features, Architecture and Speed
240+
-------------------------------------
242241

243242
I hope you're as excited about Flex as I am! But we still have *one* more chapter,
244243
and it's the most important yet. I want to show you how Symfony empowers you to quickly
@@ -247,3 +246,4 @@ the service container, and it's Symfony's super power.
247246

248247
.. _`https://symfony.sh`: https://symfony.sh
249248
.. _`Api Platform`: https://api-platform.com/
249+
.. _`Twig`: https://twig.symfony.com/

quick_tour/the_architecture.rst

Lines changed: 183 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Want a logging system? No problem:
1919
$ composer require logger
2020
2121
This installs and configures (via a recipe) the powerful `Monolog`_ library. To
22-
use the logger in a controller, add a new argument type-hinted with ``LoggerInterface``:
22+
use the logger in a controller, add a new argument type-hinted with ``LoggerInterface``::
2323

2424
use Psr\Log\LoggerInterface;
2525
// ...
@@ -71,7 +71,7 @@ Creating Services
7171

7272
To keep your code organized, you can even create your own services! Suppose you
7373
want to generate a random greeting (e.g. "Hello", "Yo", etc). Instead of putting
74-
this code directly in your controller, create a new class:
74+
this code directly in your controller, create a new class::
7575

7676
// src/GreetingGenerator.php
7777
namespace App;
@@ -129,11 +129,189 @@ difference is that it's done in the constructor:
129129
}
130130
}
131131
132-
Yes! This works too: no configuration, no time wasted.
132+
Yes! This works too: no configuration, no time wasted. Keep coding!
133133

134-
Event Subscriber
135-
----------------
134+
Twig Extension & Autoconfiguration
135+
----------------------------------
136136

137+
Thanks to Symfony's service handling, you can *extend* Symfony in many ways, like
138+
by creating an event subscriber or or a security voter for complex authorization
139+
rules. Let's add a new filter to Twig called ``greet``. How? Just create a class
140+
that extends ``AbstractExtension``::
137141

142+
// src/Twig/GreetExtension.php
143+
namespace App\Twig;
144+
145+
use App\GreetingGenerator;
146+
use Twig\Extension\AbstractExtension;
147+
use Twig\TwigFilter;
148+
149+
class GreetExtension extends AbstractExtension
150+
{
151+
private $greetingGenerator;
152+
153+
public function __construct(GreetingGenerator $greetingGenerator)
154+
{
155+
$this->greetingGenerator = $greetingGenerator;
156+
}
157+
158+
public function getFilters()
159+
{
160+
return [
161+
new TwigFilter('greet', [$this, 'greetUser']),
162+
];
163+
}
164+
165+
public function greetUser($name)
166+
{
167+
$greeting = $this->greetingGenerator->getRandomGreeting();
168+
169+
return "$greeting $name!";
170+
}
171+
}
172+
173+
After creating just *one* file, you can use this immediately:
174+
175+
.. code-block:: twig
176+
177+
{# Will print something like "Hey Symfony!" #}
178+
<h1>{{ name|greet }}</h1>
179+
180+
How does this work? Symfony notices that your class extends ``AbstractExtension``
181+
and so *automatically* registers it as a Twig extension. This is called autoconfiguration,
182+
and it works for *many* many things. Just create a class and then extend a base class
183+
(or implement an interface). Symfony takes care of the rest.
184+
185+
Blazing Speed: The Cached Container
186+
-----------------------------------
187+
188+
After seeing how much Symfony handles automatically, you might be wondering: "Doesn't
189+
this hurt performance?" Actually, no! Symfony is blazing fast.
190+
191+
How is that possible? The service system is managed by a very important object called
192+
the "container". Most frameworks have a container, but Symfony's is unique because
193+
it's *cached*. When you loaded your first page, all of the service information was
194+
compiled and saved. This means that the autowiring and autoconfiguration features
195+
add *no* overhead! It also means that you get *great* errors: Symfony inspects and
196+
validates *everything* when the container is built.
197+
198+
Now you might be wondering what happens when you update a file and the cache needs
199+
to rebuild? I like you're thinking! It's smart enough to rebuild on the next page
200+
load. But that's really the topic of the next section.
201+
202+
Development Versus Production: Environments
203+
-------------------------------------------
204+
205+
One of a framework's main jobs is to make debugging easy! And our app is *full* of
206+
great tools for this: the web debug toolbar displays at the bottom of the page, errors
207+
are big, beautiful & explicit, and any configuration cache is automatically rebuilt
208+
whenever needed.
209+
210+
But what about when you deploy to production? We will need to hide those tool and
211+
optimize for speed!
212+
213+
This is solved by Symfony's *environment* system and there are three: ``dev``, ``prod``
214+
and ``test``. Based on the environment, Symfony loads different files in the ``config/``
215+
directory:
216+
217+
.. code-block:: text
218+
219+
config/
220+
├─ services.yaml
221+
├─ ...
222+
└─ packages/
223+
├─ framework.yaml
224+
├─ ...
225+
├─ **dev/**
226+
├─ monolog.yaml
227+
└─ ...
228+
├─ **prod/**
229+
└─ monolog.yaml
230+
└─ **test/**
231+
├─ framework.yaml
232+
└─ ...
233+
└─ routes/
234+
├─ annotations.yaml
235+
└─ **dev/**
236+
├─ twig.yaml
237+
└─ web_profiler.yaml
238+
239+
This is a *powerful* idea: by changing one piece of configuration (the environment),
240+
your app is transformed from a debugging-friendly experience to one that's optimized
241+
for speed.
242+
243+
Oh, how do you change the environment? Change the ``APP_ENV`` environment variable
244+
from ``dev`` to ``prod``:
245+
246+
.. code-block:: diff
247+
248+
# .env
249+
- APP_ENV=dev
250+
+ APP_ENV=prod
251+
252+
But I want to talk more about environment variables next. Change the value back
253+
to ``dev``: debugging tools are great when you're working locally.
254+
255+
Environment Variables
256+
---------------------
257+
258+
Every app contains configuration that's different on each server - like database
259+
connection information or passwords. How should these be stored? In files? Or some
260+
other way?
261+
262+
Symfony follows the industry best practice by storing server-based configuration
263+
as *environment* variables. This means that Symfony works *perfectly* with
264+
Platform as a Service (PaaS) deployment systems as well as Docker.
265+
266+
But setting environment variables while developing can be a pain. That's why your
267+
app automatically loads a ``.env`` file, if the ``APP_ENV`` environment variable
268+
isn't set in the environment. The keys in this file then become environment variables
269+
are and read by your app:
270+
271+
.. code-block:: bash
272+
273+
# .env
274+
###> symfony/framework-bundle ###
275+
APP_ENV=dev
276+
APP_SECRET=cc86c7ca937636d5ddf1b754beb22a10
277+
###< symfony/framework-bundle ###
278+
279+
At first, the file doesn't contain much. But as your app grows, you'll add more
280+
configuration as you need it. But, actually, it gets much more interesting! Suppose
281+
your app needs database ORM. Let's install the Doctrine ORM:
282+
283+
.. code-block:: terminal
284+
285+
$ composer require doctrine
286+
287+
Thanks to a new recipe installed by Flex, look at the ``.env`` file again:
288+
289+
.. code-block:: diff
290+
291+
###> symfony/framework-bundle ###
292+
APP_ENV=dev
293+
APP_SECRET=cc86c7ca937636d5ddf1b754beb22a10
294+
###< symfony/framework-bundle ###
295+
296+
+ ###> doctrine/doctrine-bundle ###
297+
+ # ...
298+
+ DATABASE_URL=mysql://db_user:[email protected]:3306/db_name
299+
+ ###< doctrine/doctrine-bundle ###
300+
301+
The new ``DATBASE_URL`` environment variable was added *automatically* and is already
302+
referenced by the new ``doctrine.yaml`` configuration file. By combining environment
303+
variables and Flex, you're using industry best practices without any extra effort.
304+
305+
Keep Going!
306+
-----------
307+
308+
Call me crazy, but after reading this part, you should be comfortable with the most
309+
*important* parts of Symfony. Everything in Symfony is designed to get out of your
310+
way so you can keep coding and adding features, all with the speed and quality you
311+
demand.
312+
313+
That's all for the quick tour. From authentication, to forms, to caching, there is
314+
so much more to discover. Ready to dig into these topics now? Look no further - go
315+
to the official :doc:`/index` and pick any guide you want.
138316

139317
.. _`Monolog`: https://github.com/Seldaek/monolog

quick_tour/the_big_picture.rst

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ most important concepts and start building a real project!
66

77
If you've used a web framework before, you should feel right at home with
88
Symfony. If not, welcome to a whole new way of developing web applications. Symfony
9-
*embraces* best practices, keeps backwards compatbility (Yes! Upgrading is always
9+
*embraces* best practices, keeps backwards compatibility (Yes! Upgrading is always
1010
safe & easy!) and offers long-term support.
1111

1212
.. _installing-symfony2:
@@ -22,13 +22,12 @@ Ready? In a terminal, run:
2222
2323
$ composer create-project symfony/skeleton quick_tour
2424
25-
This creates a new ``quick_tour/`` directory with, at first, just *one* file:
26-
``composer.json``. But when the command finishes, you'll find a small, but
27-
powerful Symfony application:
25+
This creates a new ``quick_tour/`` directory with a small, but powerful new
26+
Symfony application:
2827

2928
.. code-block:: text
3029
31-
symfony-project/
30+
quick_tour/
3231
├─ .env
3332
├─ .env.dist
3433
├─ bin/console
@@ -41,13 +40,10 @@ powerful Symfony application:
4140
├─ var/
4241
└─ vendor/
4342
44-
Where did these files come from? They came from recipes installed by Symfony Flex.
45-
But don't worry about that yet: we'll talk about it soon.
46-
4743
Can we already load the project in a browser? Of course! You can setup
48-
:doc:`Nginx or Apache <setup/web_server_configuration>` and configure their document
44+
:doc:`Nginx or Apache </setup/web_server_configuration>` and configure their document
4945
root to be the ``public/`` directory. But, for development, Symfony has its own server.
50-
Install it with:
46+
Install and run it with:
5147

5248
.. code-block:: terminal
5349
@@ -179,4 +175,6 @@ in ``DefaultController``::
179175
Routing can do *even* more, but we'll save that for another time! Right now, our
180176
app needs more features! Like a template engine, logging, debugging tools and more.
181177

182-
Keep reading with :doc:`/quick_tour/flex_architecture`.
178+
Keep reading with :doc:`/quick_tour/flex_recipes`.
179+
180+
.. _`Composer`: https://getcomposer.org/

routing.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,32 @@ So how can you make ``blog_list`` once again match when the user visits
327327
Now, when the user visits ``/blog``, the ``blog_list`` route will match and
328328
``$page`` will default to a value of ``1``.
329329

330+
Listing all of your Routes
331+
--------------------------
332+
333+
As your app grows, you'll eventually have a *lot* of routes! To see them all, run:
334+
335+
.. code-block:: terminal
336+
337+
$ php bin/console debug:router
338+
339+
.. code-block:: text
340+
341+
------------------------------ -------- -------------------------------------
342+
Name Method Path
343+
------------------------------ -------- -------------------------------------
344+
app_lucky_number ANY /lucky/number/{max}
345+
...
346+
------------------------------ -------- -------------------------------------
347+
330348
.. index::
331349
single: Routing; Advanced example
332350
single: Routing; _format parameter
333351

334352
.. _advanced-routing-example:
335353

336354
Advanced Routing Example
337-
~~~~~~~~~~~~~~~~~~~~~~~~
355+
------------------------
338356

339357
With all of this in mind, check out this advanced example:
340358

0 commit comments

Comments
 (0)