@@ -7,13 +7,13 @@ Create your First Page in Symfony
7
7
Creating a new page - whether it's an HTML page or a JSON endpoint - is a
8
8
two-step process:
9
9
10
- #. **Create a route **: A route is the URL (e.g. ``/about ``) to your page and
11
- points to a controller;
12
-
13
10
#. **Create a controller **: A controller is the PHP function you write that
14
11
builds the page. You take the incoming request information and use it to
15
12
create a Symfony ``Response `` object, which can hold HTML content, a JSON
16
- string or even a binary file like an image or PDF.
13
+ string or even a binary file like an image or PDF;
14
+
15
+ #. **Create a route **: A route is the URL (e.g. ``/about ``) to your page and
16
+ points to a controller.
17
17
18
18
.. admonition :: Screencast
19
19
:class: screencast
@@ -55,47 +55,12 @@ random) number and prints it. To do that, create a "Controller" class and a
55
55
}
56
56
}
57
57
58
- Now you need to associate this controller function with a public URL (e.g. ``/lucky/number ``)
59
- so that the ``number() `` method is called when a user browses to it. This association
60
- is defined by creating a **route ** in the ``config/routes.yaml `` file:
61
-
62
- .. code-block :: yaml
63
-
64
- # config/routes.yaml
65
-
66
- # the "app_lucky_number" route name is not important yet
67
- app_lucky_number :
68
- path : /lucky/number
69
- controller : App\Controller\LuckyController::number
70
-
71
- That's it! If you are using Symfony web server, try it out by going to: http://localhost:8000/lucky/number
72
-
73
- If you see a lucky number being printed back to you, congratulations! But before
74
- you run off to play the lottery, check out how this works. Remember the two steps
75
- to create a page?
76
-
77
- #. *Create a controller and a method *: This is a function where *you * build the page and ultimately
78
- return a ``Response `` object. You'll learn more about :doc: `controllers </controller >`
79
- in their own section, including how to return JSON responses;
80
-
81
- #. *Create a route *: In ``config/routes.yaml ``, the route defines the URL to your
82
- page (``path ``) and what ``controller `` to call. You'll learn more about :doc: `routing </routing >`
83
- in its own section, including how to make *variable * URLs.
84
-
85
58
.. _annotation-routes :
86
59
87
- Annotation Routes
88
- -----------------
89
-
90
- Instead of defining your route in YAML, Symfony also allows you to use *annotation *
91
- or *attribute * routes. Attributes are built-in in PHP starting from PHP 8. In earlier
92
- PHP versions you can use annotations. To do this, install the annotations package:
93
-
94
- .. code-block :: terminal
95
-
96
- $ composer require annotations
97
-
98
- You can now add your route directly *above * the controller:
60
+ Now you need to associate this controller function with a public URL (e.g. ``/lucky/number ``)
61
+ so that the ``number() `` method is called when a user browses to it. This association
62
+ is defined with the ``#[Route] `` attribute (in PHP, `attributes `_ are used to add
63
+ metadata to code):
99
64
100
65
.. configuration-block ::
101
66
@@ -115,27 +80,25 @@ You can now add your route directly *above* the controller:
115
80
}
116
81
}
117
82
118
- That's it! The page - http://localhost:8000/lucky/number will work exactly
119
- like before! Annotations/attributes are the recommended way to configure routes.
83
+ That's it! If you are using Symfony web server, try it out by going to: http://localhost:8000/lucky/number
120
84
121
- .. _ flex-quick-intro :
85
+ .. tip : :
122
86
123
- Auto-Installing Recipes with Symfony Flex
124
- -----------------------------------------
87
+ Symfony recommends defining routes as attributes to have the controller code
88
+ and its route configuration at the same location. However, if you prefer, you can
89
+ :doc: `define routes in separate files </routing >` using YAML, XML and PHP formats.
125
90
126
- You may not have noticed, but when you ran `` composer require annotations ``, two
127
- special things happened, both thanks to a powerful Composer plugin called
128
- :ref: ` Flex < symfony-flex >`.
91
+ If you see a lucky number being printed back to you, congratulations! But before
92
+ you run off to play the lottery, check out how this works. Remember the two steps
93
+ to create a page?
129
94
130
- First, ``annotations `` isn't a real package name: it's an *alias * (i.e. shortcut)
131
- that Flex resolves to ``sensio/framework-extra-bundle ``.
95
+ #. *Create a controller and a method *: This is a function where *you * build the page and ultimately
96
+ return a ``Response `` object. You'll learn more about :doc: `controllers </controller >`
97
+ in their own section, including how to return JSON responses;
132
98
133
- Second, after this package was downloaded, Flex runs a *recipe *, which is a
134
- set of automated instructions that tell Symfony how to integrate an external
135
- package. `Flex recipes `_ exist for many packages and have the ability
136
- to do a lot, like adding configuration files, creating directories, updating ``.gitignore ``
137
- and adding a new config to your ``.env `` file. Flex *automates * the installation of
138
- packages so you can get back to coding.
99
+ #. *Create a route *: In ``config/routes.yaml ``, the route defines the URL to your
100
+ page (``path ``) and what ``controller `` to call. You'll learn more about :doc: `routing </routing >`
101
+ in its own section, including how to make *variable * URLs.
139
102
140
103
The bin/console Command
141
104
-----------------------
@@ -343,4 +306,4 @@ Go Deeper with HTTP & Framework Fundamentals
343
306
.. _`Twig` : https://twig.symfony.com
344
307
.. _`Composer` : https://getcomposer.org
345
308
.. _`Harmonious Development with Symfony` : https://symfonycasts.com/screencast/symfony/setup
346
- .. _`Flex recipes ` : https://github.com/symfony/recipes/blob/flex/main/RECIPES.md
309
+ .. _`attributes ` : https://www.php.net/manual/en/language.attributes.overview.php
0 commit comments