@@ -39,7 +39,7 @@ Symfony application:
39
39
├─ var/
40
40
└─ vendor/
41
41
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
43
43
:doc: `Nginx or Apache </setup/web_server_configuration >` and configure their
44
44
document root to be the ``public/ `` directory. But, for development, it's better
45
45
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.
63
63
64
64
But before we go too far, let's dig into the fundamentals by building our first page.
65
65
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
-
80
66
In ``src/Controller ``, create a new ``DefaultController `` class and an ``index ``
81
67
method inside::
82
68
83
69
// src/Controller/DefaultController.php
84
70
namespace App\Controller;
85
71
86
72
use Symfony\Component\HttpFoundation\Response;
73
+ use Symfony\Component\Routing\Attribute\Route;
87
74
88
75
class DefaultController
89
76
{
77
+ #[Route('/', name: 'index')]
90
78
public function index(): Response
91
79
{
92
80
return new Response('Hello!');
@@ -104,11 +92,21 @@ But the routing system is *much* more powerful. So let's make the route more int
104
92
105
93
.. code-block :: diff
106
94
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
+ }
112
110
113
111
The URL to this page has changed: it is *now * ``/hello/* ``: the ``{name} `` acts
114
112
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
120
118
namespace App\Controller;
121
119
122
120
use Symfony\Component\HttpFoundation\Response;
121
+ use Symfony\Component\Routing\Attribute\Route;
123
122
124
123
class DefaultController
125
124
{
125
+ #[Route('/hello/{name}', name: 'index')]
126
126
- public function index()
127
127
+ public function index(string $name): Response
128
128
{
@@ -135,39 +135,8 @@ Try the page out by going to ``http://localhost:8000/hello/Symfony``. You should
135
135
see: Hello Symfony! The value of the ``{name} `` in the URL is available as a ``$name ``
136
136
argument in your controller.
137
137
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 ``::
171
140
172
141
// src/Controller/DefaultController.php
173
142
namespace App\Controller;
0 commit comments