Skip to content

Commit d347056

Browse files
committed
rewrite the routing introduction
1 parent d90abb5 commit d347056

File tree

1 file changed

+22
-53
lines changed

1 file changed

+22
-53
lines changed

quick_tour/the_big_picture.rst

+22-53
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Symfony application:
3939
├─ var/
4040
└─ vendor/
4141
42-
Can we already load the project in a browser? Yes! You can setup
42+
Can we already load the project in a browser? Yes! You can set up
4343
:doc:`Nginx or Apache </setup/web_server_configuration>` and configure their
4444
document root to be the ``public/`` directory. But, for development, it's better
4545
to :doc:`install the Symfony local web server </setup/symfony_server>` and run
@@ -63,30 +63,18 @@ web app, or a microservice. Symfony starts small, but scales with you.
6363

6464
But before we go too far, let's dig into the fundamentals by building our first page.
6565

66-
Start in ``config/routes.yaml``: this is where *we* can define the URL to our new
67-
page. Uncomment the example that already lives in the file:
68-
69-
.. code-block:: yaml
70-
71-
# config/routes.yaml
72-
index:
73-
path: /
74-
controller: 'App\Controller\DefaultController::index'
75-
76-
This is called a *route*: it defines the URL to your page (``/``) and the "controller":
77-
the *function* that will be called whenever anyone goes to this URL. That function
78-
doesn't exist yet, so let's create it!
79-
8066
In ``src/Controller``, create a new ``DefaultController`` class and an ``index``
8167
method inside::
8268

8369
// src/Controller/DefaultController.php
8470
namespace App\Controller;
8571

8672
use Symfony\Component\HttpFoundation\Response;
73+
use Symfony\Component\Routing\Attribute\Route;
8774

8875
class DefaultController
8976
{
77+
#[Route('/', name: 'index')]
9078
public function index(): Response
9179
{
9280
return new Response('Hello!');
@@ -104,11 +92,21 @@ But the routing system is *much* more powerful. So let's make the route more int
10492

10593
.. code-block:: diff
10694
107-
# config/routes.yaml
108-
index:
109-
- path: /
110-
+ path: /hello/{name}
111-
controller: 'App\Controller\DefaultController::index'
95+
// src/Controller/DefaultController.php
96+
namespace App\Controller;
97+
98+
use Symfony\Component\HttpFoundation\Response;
99+
use Symfony\Component\Routing\Attribute\Route;
100+
101+
class DefaultController
102+
{
103+
- #[Route('/', name: 'index')]
104+
+ #[Route('/hello/{name}', name: 'index')]
105+
public function index(): Response
106+
{
107+
return new Response('Hello!');
108+
}
109+
}
112110
113111
The URL to this page has changed: it is *now* ``/hello/*``: the ``{name}`` acts
114112
like a wildcard that matches anything. And it gets better! Update the controller too:
@@ -120,9 +118,11 @@ like a wildcard that matches anything. And it gets better! Update the controller
120118
namespace App\Controller;
121119
122120
use Symfony\Component\HttpFoundation\Response;
121+
use Symfony\Component\Routing\Attribute\Route;
123122
124123
class DefaultController
125124
{
125+
#[Route('/hello/{name}', name: 'index')]
126126
- public function index()
127127
+ public function index(string $name): Response
128128
{
@@ -135,39 +135,8 @@ Try the page out by going to ``http://localhost:8000/hello/Symfony``. You should
135135
see: Hello Symfony! The value of the ``{name}`` in the URL is available as a ``$name``
136136
argument in your controller.
137137

138-
But this can be even simpler! Comment-out the YAML route by adding the
139-
``#`` character:
140-
141-
.. code-block:: yaml
142-
143-
# config/routes.yaml
144-
# index:
145-
# path: /hello/{name}
146-
# controller: 'App\Controller\DefaultController::index'
147-
148-
Instead, add the route *right above* the controller method:
149-
150-
.. code-block:: diff
151-
152-
<?php
153-
// src/Controller/DefaultController.php
154-
namespace App\Controller;
155-
156-
use Symfony\Component\HttpFoundation\Response;
157-
+ use Symfony\Component\Routing\Attribute\Route;
158-
159-
class DefaultController
160-
{
161-
+ #[Route('/hello/{name}', methods: ['GET'])]
162-
public function index(string $name): Response
163-
{
164-
// ...
165-
}
166-
}
167-
168-
This works just like before! But by using attributes, the route and controller
169-
live right next to each other. Need another page? Add another route and method
170-
in ``DefaultController``::
138+
But by using attributes, the route and controller live right next to each
139+
other. Need another page? Add another route and method in ``DefaultController``::
171140

172141
// src/Controller/DefaultController.php
173142
namespace App\Controller;

0 commit comments

Comments
 (0)