Skip to content

Commit 7ab953c

Browse files
committed
Made all examples of "Quick Tour" complete (all "use", "namespaces", etc.)
1 parent a7e94f4 commit 7ab953c

File tree

3 files changed

+91
-36
lines changed

3 files changed

+91
-36
lines changed

quick_tour/flex_recipes.rst

+22-11
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ Thanks to Flex, after one command, you can start using Twig immediately:
7676
.. code-block:: diff
7777
7878
// src/Controller/DefaultController.php
79-
// ...
79+
namespace App\Controller;
8080
81+
use Symfony\Component\Routing\Annotation\Route;
82+
- use Symfony\Component\HttpFoundation\Response;
8183
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8284
8385
-class DefaultController
@@ -150,15 +152,26 @@ Rich API Support
150152

151153
Are you building an API? You can already return JSON easily from any controller::
152154

153-
/**
154-
* @Route("/api/hello/{name}")
155-
*/
156-
public function apiExample($name)
155+
// src/Controller/DefaultController.php
156+
namespace App\Controller;
157+
158+
use Symfony\Component\Routing\Annotation\Route;
159+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
160+
161+
class DefaultController extends AbstractController
157162
{
158-
return $this->json([
159-
'name' => $name,
160-
'symfony' => 'rocks',
161-
]);
163+
// ...
164+
165+
/**
166+
* @Route("/api/hello/{name}")
167+
*/
168+
public function apiExample($name)
169+
{
170+
return $this->json([
171+
'name' => $name,
172+
'symfony' => 'rocks',
173+
]);
174+
}
162175
}
163176

164177
But for a *truly* rich API, try installing `Api Platform`_:
@@ -176,8 +189,6 @@ rich API for a ``product`` table? Create a ``Product`` entity and give it the
176189
``@ApiResource()`` annotation::
177190

178191
// src/Entity/Product.php
179-
// ...
180-
181192
namespace App\Entity;
182193

183194
use ApiPlatform\Core\Annotation\ApiResource;

quick_tour/the_architecture.rst

+32-9
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,24 @@ Want a logging system? No problem:
2121
This installs and configures (via a recipe) the powerful `Monolog`_ library. To
2222
use the logger in a controller, add a new argument type-hinted with ``LoggerInterface``::
2323

24+
// src/Controller/DefaultController.php
25+
namespace App\Controller;
26+
2427
use Psr\Log\LoggerInterface;
25-
// ...
28+
use Symfony\Component\Routing\Annotation\Route;
29+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2630

27-
public function index($name, LoggerInterface $logger)
31+
class DefaultController extends AbstractController
2832
{
29-
$logger->info("Saying hello to $name!");
33+
/**
34+
* @Route("/hello/{name}")
35+
*/
36+
public function index($name, LoggerInterface $logger)
37+
{
38+
$logger->info("Saying hello to $name!");
3039

31-
// ...
40+
// ...
41+
}
3242
}
3343

3444
That's it! The new log message will be written to ``var/log/dev.log``. Of course, this
@@ -89,16 +99,27 @@ this code directly in your controller, create a new class::
8999

90100
Great! You can use this immediately in your controller::
91101

102+
// src/Controller/DefaultController.php
103+
namespace App\Controller;
104+
92105
use App\GreetingGenerator;
93-
// ...
106+
use Psr\Log\LoggerInterface;
107+
use Symfony\Component\Routing\Annotation\Route;
108+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
94109

95-
public function index($name, LoggerInterface $logger, GreetingGenerator $generator)
110+
class DefaultController extends AbstractController
96111
{
97-
$greeting = $generator->getRandomGreeting();
112+
/**
113+
* @Route("/hello/{name}")
114+
*/
115+
public function index($name, LoggerInterface $logger, GreetingGenerator $generator)
116+
{
117+
$greeting = $generator->getRandomGreeting();
98118

99-
$logger->info("Saying $greeting to $name!");
119+
$logger->info("Saying $greeting to $name!");
100120

101-
// ...
121+
// ...
122+
}
102123
}
103124

104125
That's it! Symfony will instantiate the ``GreetingGenerator`` automatically and
@@ -108,6 +129,7 @@ difference is that it's done in the constructor:
108129

109130
.. code-block:: diff
110131
132+
// src/GreetingGenerator.php
111133
+ use Psr\Log\LoggerInterface;
112134
113135
class GreetingGenerator
@@ -174,6 +196,7 @@ After creating just *one* file, you can use this immediately:
174196

175197
.. code-block:: twig
176198
199+
{# templates/default/index.html.twig #}
177200
{# Will print something like "Hey Symfony!" #}
178201
<h1>{{ name|greet }}</h1>
179202

quick_tour/the_big_picture.rst

+37-16
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ page. Uncomment the example that already lives in the file:
6969

7070
.. code-block:: yaml
7171
72+
# config/routes.yaml
7273
index:
7374
path: /
7475
controller: 'App\Controller\DefaultController::index'
@@ -80,6 +81,7 @@ doesn't exist yet, so let's create it!
8081
In ``src/Controller``, create a new ``DefaultController`` class and an ``index``
8182
method inside::
8283

84+
// src/Controller/DefaultController.php
8385
namespace App\Controller;
8486

8587
use Symfony\Component\HttpFoundation\Response;
@@ -115,13 +117,18 @@ like a wildcard that matches anything. And it gets better! Update the controller
115117
.. code-block:: diff
116118
117119
// src/Controller/DefaultController.php
118-
// ...
120+
namespace App\Controller;
121+
122+
use Symfony\Component\HttpFoundation\Response;
119123
120-
- public function index()
121-
+ public function index($name)
124+
class DefaultController
122125
{
123-
- return new Response('Hello!');
124-
+ return new Response("Hello $name!");
126+
- public function index()
127+
+ public function index($name)
128+
{
129+
- return new Response('Hello!');
130+
+ return new Response("Hello $name!");
131+
}
125132
}
126133
127134
Try the page out by going to ``http://localhost:8000/hello/Symfony``. You should
@@ -148,28 +155,42 @@ Instead, add the route *right above* the controller method:
148155
.. code-block:: diff
149156
150157
// src/Controller/DefaultController.php
151-
// ...
158+
namespace App\Controller;
152159
160+
use Symfony\Component\HttpFoundation\Response;
153161
+ use Symfony\Component\Routing\Annotation\Route;
154162
155-
+ /**
156-
+ * @Route("/hello/{name}")
157-
+ */
158-
public function index($name)
163+
class DefaultController
164+
{
165+
+ /**
166+
+ * @Route("/hello/{name}")
167+
+ */
168+
public function index($name) {
169+
// ...
170+
}
171+
}
159172
160173
This works just like before! But by using annotations, the route and controller
161174
live right next to each other. Need another page? Just add another route and method
162175
in ``DefaultController``::
163176

164177
// src/Controller/DefaultController.php
165-
// ...
178+
namespace App\Controller;
179+
180+
use Symfony\Component\HttpFoundation\Response;
181+
use Symfony\Component\Routing\Annotation\Route;
166182

167-
/**
168-
* @Route("/simplicity")
169-
*/
170-
public function simple()
183+
class DefaultController
171184
{
172-
return new Response('Simple! Easy! Great!');
185+
// ...
186+
187+
/**
188+
* @Route("/simplicity")
189+
*/
190+
public function simple()
191+
{
192+
return new Response('Simple! Easy! Great!');
193+
}
173194
}
174195

175196
Routing can do *even* more, but we'll save that for another time! Right now, our

0 commit comments

Comments
 (0)