@@ -17,39 +17,78 @@ Creating Routes
1717
1818Routes can be configured in YAML, XML, PHP or using annotations. All formats
1919provide the same features and performance, so choose your favorite.
20- :doc: `Symfony recommends annotations </best_practices/controllers >`
21- because it's convenient to put the route and controller in the
22- same place instead of dealing with multiple files.
20+ :doc: `Symfony recommends annotations </best_practices/controllers >` because it's
21+ convenient to put the route and controller in the same place.
2322
24- If you choose annotations, run this command once in your application to add
25- support for them:
23+ Creating Routes as Annotations
24+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25+
26+ Run this command once in your application to add support for annotations:
2627
2728.. code-block :: terminal
2829
2930 $ composer require annotations
3031
31- Suppose you want to define a route for the ``/blog `` URL in your application:
32+ In addition to installing the needed dependencies, this command creates the
33+ following configuration file:
3234
33- .. configuration -block ::
35+ .. code -block :: yaml
3436
35- .. code-block :: php-annotations
37+ # config/routes.yaml
38+ controllers :
39+ resource : ' ../src/Controller/'
40+ type : annotation
3641
37- // src/Controller/BlogController.php
38- namespace App\ Controller;
42+ This configuration tells Symfony to looks for routes defined as annotations in
43+ any PHP class stored in the `` src/ Controller/ `` directory.
3944
40- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
41- use Symfony\Component\Routing\Annotation\Route;
45+ Suppose you want to define a route for the `` /blog `` URL in your application. To
46+ do so, create a :doc: ` controller class < /controller >` like the following::
4247
43- class BlogController extends AbstractController
48+ // src/Controller/BlogController.php
49+ namespace App\Controller;
50+
51+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
52+ use Symfony\Component\Routing\Annotation\Route;
53+
54+ class BlogController extends AbstractController
55+ {
56+ /**
57+ * @Route("/blog", name="blog_list")
58+ */
59+ public function list()
4460 {
45- /**
46- * @Route("/blog", name="blog_list")
47- */
48- public function list()
49- {
50- // ...
51- }
61+ // ...
5262 }
63+ }
64+
65+ This configuration defines a route called ``blog_list `` that matches when the
66+ user requests the ``/blog `` URL. When the match occurs, the application runs
67+ the ``list() `` method of the ``BlogController `` class.
68+
69+ .. note ::
70+
71+ The query string of a URL is not considered when matching routes. In this
72+ example, URLs like ``/blog?foo=bar `` and ``/blog?foo=bar&bar=foo `` will
73+ also match the ``blog_list `` route.
74+
75+ The route name (``blog_list ``) is not important for now, but it will be
76+ essential later when :ref: `generating URLs <routing-generating-urls >`. You only
77+ have to keep in mind that each route name must be unique in the application.
78+
79+ Creating Routes in YAML, XML or PHP Files
80+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81+
82+ Instead of defining routes in the controller classes, you can define them in a
83+ separate YAML, XML or PHP file. The main advantage is that they don't require
84+ any extra dependency. The main drawback is that you have to work with multiple
85+ files when checking the routing of some controller action.
86+
87+ The following example shows how to define in YAML/XML/PHP a route called
88+ ``blog_list `` that associates the ``/blog `` URL with the ``list() `` action of
89+ the ``BlogController ``:
90+
91+ .. configuration-block ::
5392
5493 .. code-block :: yaml
5594
@@ -98,20 +137,6 @@ Suppose you want to define a route for the ``/blog`` URL in your application:
98137 ;
99138 };
100139
101- This configuration defines a route called ``blog_list `` that matches when the
102- user requests the ``/blog `` URL. When the match occurs, the application runs
103- the ``list() `` method of the ``BlogController `` class.
104-
105- .. note ::
106-
107- The query string of a URL is not considered when matching routes. In this
108- example, URLs like ``/blog?foo=bar `` and ``/blog?foo=bar&bar=foo `` will
109- also match the ``blog_list `` route.
110-
111- The route name (``blog_list ``) is not important for now, but it will be essential later when
112- :ref: `generating URLs <routing-generating-urls >`. You only have to keep in mind
113- that each route name must be unique in the application.
114-
115140 .. _routing-matching-http-methods :
116141
117142Matching HTTP Methods
0 commit comments