Skip to content

Commit ab002d9

Browse files
committed
feature #8657 Updated routing articles to Symfony 4 (javiereguiluz)
This PR was squashed before being merged into the master branch (closes #8657). Discussion ---------- Updated routing articles to Symfony 4 This fixes #8656. Commits ------- 419353b Used the modern "controller" key in YAML routes 6d7415f Updated routing articles to Symfony 4
2 parents 91562c6 + 419353b commit ab002d9

12 files changed

+199
-181
lines changed

routing/conditions.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,37 @@ define arbitrary matching logic, use the ``conditions`` routing option:
1212

1313
.. code-block:: yaml
1414
15+
# config/routes.yaml
1516
contact:
16-
path: /contact
17-
defaults: { _controller: AcmeDemoBundle:Main:contact }
18-
condition: "context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i'"
17+
path: /contact
18+
controller: 'App\Controller\DefaultController::contact'
19+
condition: "context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i'"
1920
2021
.. code-block:: xml
2122
23+
<!-- config/routes.xml -->
2224
<?xml version="1.0" encoding="UTF-8" ?>
2325
<routes xmlns="http://symfony.com/schema/routing"
2426
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2527
xsi:schemaLocation="http://symfony.com/schema/routing
2628
http://symfony.com/schema/routing/routing-1.0.xsd">
2729
2830
<route id="contact" path="/contact">
29-
<default key="_controller">AcmeDemoBundle:Main:contact</default>
31+
<default key="_controller">App\Controller\DefaultController::contact</default>
3032
<condition>context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i'</condition>
3133
</route>
3234
</routes>
3335
3436
.. code-block:: php
3537
38+
// config/routes.php
3639
use Symfony\Component\Routing\RouteCollection;
3740
use Symfony\Component\Routing\Route;
3841
3942
$collection = new RouteCollection();
4043
$collection->add('contact', new Route(
4144
'/contact', array(
42-
'_controller' => 'AcmeDemoBundle:Main:contact',
45+
'_controller' => 'App\Controller\DefaultController::contact',
4346
),
4447
array(),
4548
array(),

routing/custom_route_loader.rst

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ and therefore have two important methods:
3636
:method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::supports`
3737
and :method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::load`.
3838

39-
Take these lines from the ``routing.yml`` in the Symfony Standard Edition:
39+
Take these lines from the ``routes.yaml`` in the Symfony Standard Edition:
4040

4141
.. code-block:: yaml
4242
4343
# config/routes.yaml
44-
app:
45-
resource: '@AppBundle/Controller/'
46-
type: annotation
44+
controllers:
45+
resource: ../src/Controller/
46+
type: annotation
4747
4848
When the main loader parses this, it tries all registered delegate loaders and calls
4949
their :method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::supports`
50-
method with the given resource (``@AppBundle/Controller/``)
50+
method with the given resource (``../src/Controller/``)
5151
and type (``annotation``) as arguments. When one of the loader returns ``true``,
5252
its :method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::load` method
5353
will be called, which should return a :class:`Symfony\\Component\\Routing\\RouteCollection`
@@ -56,7 +56,7 @@ containing :class:`Symfony\\Component\\Routing\\Route` objects.
5656
.. note::
5757

5858
Routes loaded this way will be cached by the Router the same way as
59-
when they are defined in one of the default formats (e.g. XML, YML,
59+
when they are defined in one of the default formats (e.g. XML, YAML,
6060
PHP file).
6161

6262
Creating a custom Loader
@@ -97,7 +97,7 @@ you do. The resource name itself is not actually used in the example::
9797
// prepare a new route
9898
$path = '/extra/{parameter}';
9999
$defaults = array(
100-
'_controller' => 'AppBundle:Extra:extra',
100+
'_controller' => 'App\Controller\ExtraController::extra',
101101
);
102102
$requirements = array(
103103
'parameter' => '\d+',
@@ -120,8 +120,7 @@ you do. The resource name itself is not actually used in the example::
120120
}
121121

122122
Make sure the controller you specify really exists. In this case you
123-
have to create an ``extraAction()`` method in the ``ExtraController``
124-
of the ``AppBundle``::
123+
have to create an ``extra()`` method in the ``ExtraController``::
125124

126125
// src/Controller/ExtraController.php
127126
namespace App\Controller;
@@ -131,7 +130,7 @@ of the ``AppBundle``::
131130

132131
class ExtraController extends Controller
133132
{
134-
public function extraAction($parameter)
133+
public function extra($parameter)
135134
{
136135
return new Response($parameter);
137136
}
@@ -152,6 +151,7 @@ Now define a service for the ``ExtraLoader``:
152151
153152
.. code-block:: xml
154153
154+
<!-- config/services.xml -->
155155
<?xml version="1.0" ?>
156156
<container xmlns="http://symfony.com/schema/dic/services"
157157
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -169,6 +169,7 @@ Now define a service for the ``ExtraLoader``:
169169
170170
.. code-block:: php
171171
172+
// config/services.php
172173
use App\Routing\ExtraLoader;
173174
174175
$container
@@ -198,6 +199,7 @@ What remains to do is adding a few lines to the routing configuration:
198199
199200
.. code-block:: xml
200201
202+
<!-- config/routes.xml -->
201203
<?xml version="1.0" encoding="UTF-8" ?>
202204
<routes xmlns="http://symfony.com/schema/routing"
203205
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -217,10 +219,10 @@ What remains to do is adding a few lines to the routing configuration:
217219
218220
return $collection;
219221
220-
The important part here is the ``type`` key. Its value should be "extra" as
222+
The important part here is the ``type`` key. Its value should be ``extra`` as
221223
this is the type which the ``ExtraLoader`` supports and this will make sure
222224
its ``load()`` method gets called. The ``resource`` key is insignificant
223-
for the ``ExtraLoader``, so it is set to ".".
225+
for the ``ExtraLoader``, so it is set to ``.`` (a single dot).
224226

225227
.. note::
226228

@@ -256,7 +258,7 @@ configuration file - you can call the
256258
{
257259
$collection = new RouteCollection();
258260

259-
$resource = '@AppBundle/Resources/config/import_routing.yml';
261+
$resource = '@ThirdPartyBundle/Resources/config/routing.yaml';
260262
$type = 'yaml';
261263

262264
$importedRoutes = $this->import($resource, $type);

routing/external_resources.rst

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ This can be done by "importing" directories into the routing configuration:
1414
.. code-block:: yaml
1515
1616
# config/routes.yaml
17-
app:
18-
resource: '@AppBundle/Controller/'
19-
type: annotation # required to enable the Annotation reader for this resource
17+
controllers:
18+
resource: ../src/Controller/
19+
type: annotation
2020
2121
.. code-block:: xml
2222
@@ -27,8 +27,7 @@ This can be done by "importing" directories into the routing configuration:
2727
xsi:schemaLocation="http://symfony.com/schema/routing
2828
http://symfony.com/schema/routing/routing-1.0.xsd">
2929
30-
<!-- the type is required to enable the annotation reader for this resource -->
31-
<import resource="@AppBundle/Controller/" type="annotation"/>
30+
<import resource="../src/Controller/" type="annotation"/>
3231
</routes>
3332
3433
.. code-block:: php
@@ -38,35 +37,31 @@ This can be done by "importing" directories into the routing configuration:
3837
3938
$collection = new RouteCollection();
4039
$collection->addCollection(
41-
// second argument is the type, which is required to enable
42-
// the annotation reader for this resource
43-
$loader->import("@AppBundle/Controller/", "annotation")
40+
$loader->import("../src/Controller/", "annotation")
4441
);
4542
4643
return $collection;
4744
4845
.. note::
4946

50-
When importing resources from YAML, the key (e.g. ``app``) is meaningless.
47+
When importing resources from YAML, the key (e.g. ``controllers``) is meaningless.
5148
Just be sure that it's unique so no other lines override it.
5249

5350
The ``resource`` key loads the given routing resource. In this example the
54-
resource is a directory, where the ``@AppBundle`` shortcut syntax resolves
55-
to the full path of the AppBundle. When pointing to a directory, all files
56-
in that directory are parsed and put into the routing.
51+
resource is a directory and all files in that directory are parsed and put into
52+
the routing.
5753

5854
.. note::
5955

60-
You can also include other routing configuration files, this is often
61-
used to import the routing of third party bundles:
56+
You can also include other routing configuration files:
6257

6358
.. configuration-block::
6459

6560
.. code-block:: yaml
6661
6762
# config/routes.yaml
6863
app:
69-
resource: '@AcmeOtherBundle/Resources/config/routing.yml'
64+
resource: '@ThirdPartyBundle/Resources/config/routing.yaml'
7065
7166
.. code-block:: xml
7267
@@ -77,7 +72,7 @@ in that directory are parsed and put into the routing.
7772
xsi:schemaLocation="http://symfony.com/schema/routing
7873
http://symfony.com/schema/routing/routing-1.0.xsd">
7974
80-
<import resource="@AcmeOtherBundle/Resources/config/routing.xml" />
75+
<import resource="@ThirdPartyBundle/Resources/config/routing.xml" />
8176
</routes>
8277
8378
.. code-block:: php
@@ -87,7 +82,7 @@ in that directory are parsed and put into the routing.
8782
8883
$collection = new RouteCollection();
8984
$collection->addCollection(
90-
$loader->import("@AcmeOtherBundle/Resources/config/routing.php")
85+
$loader->import("@ThirdPartyBundle/Resources/config/routing.php")
9186
);
9287
9388
return $collection;
@@ -96,16 +91,16 @@ Prefixing Imported Routes
9691
~~~~~~~~~~~~~~~~~~~~~~~~~
9792

9893
You can also choose to provide a "prefix" for the imported routes. For example,
99-
suppose you want to prefix all routes in the AppBundle with ``/site`` (e.g.
94+
suppose you want to prefix all application routes with ``/site`` (e.g.
10095
``/site/blog/{slug}`` instead of ``/blog/{slug}``):
10196

10297
.. configuration-block::
10398

10499
.. code-block:: yaml
105100
106101
# config/routes.yaml
107-
app:
108-
resource: '@AppBundle/Controller/'
102+
controllers:
103+
resource: '../src/Controller/'
109104
type: annotation
110105
prefix: /site
111106
@@ -119,7 +114,7 @@ suppose you want to prefix all routes in the AppBundle with ``/site`` (e.g.
119114
http://symfony.com/schema/routing/routing-1.0.xsd">
120115
121116
<import
122-
resource="@AppBundle/Controller/"
117+
resource="../src/Controller/"
123118
type="annotation"
124119
prefix="/site" />
125120
</routes>
@@ -129,7 +124,7 @@ suppose you want to prefix all routes in the AppBundle with ``/site`` (e.g.
129124
// config/routes.php
130125
use Symfony\Component\Routing\RouteCollection;
131126
132-
$app = $loader->import('@AppBundle/Controller/', 'annotation');
127+
$app = $loader->import('../src/Controller/', 'annotation');
133128
$app->addPrefix('/site');
134129
135130
$collection = new RouteCollection();

routing/extra_information.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ to your controller, and as attributes of the ``Request`` object:
1515
1616
# config/routes.yaml
1717
blog:
18-
path: /blog/{page}
18+
path: /blog/{page}
19+
controller: App\Controller\BlogController::index
1920
defaults:
20-
_controller: AppBundle:Blog:index
21-
page: 1
22-
title: "Hello world!"
21+
page: 1
22+
title: "Hello world!"
2323
2424
.. code-block:: xml
2525
@@ -31,7 +31,7 @@ to your controller, and as attributes of the ``Request`` object:
3131
http://symfony.com/schema/routing/routing-1.0.xsd">
3232
3333
<route id="blog" path="/blog/{page}">
34-
<default key="_controller">AppBundle:Blog:index</default>
34+
<default key="_controller">App\Controller\BlogController::index</default>
3535
<default key="page">1</default>
3636
<default key="title">Hello world!</default>
3737
</route>
@@ -45,7 +45,7 @@ to your controller, and as attributes of the ``Request`` object:
4545
4646
$collection = new RouteCollection();
4747
$collection->add('blog', new Route('/blog/{page}', array(
48-
'_controller' => 'AppBundle:Blog:index',
48+
'_controller' => 'App\Controller\BlogController::index',
4949
'page' => 1,
5050
'title' => 'Hello world!',
5151
)));
@@ -55,16 +55,16 @@ to your controller, and as attributes of the ``Request`` object:
5555
Now, you can access this extra parameter in your controller, as an argument
5656
to the controller method::
5757

58-
public function indexAction($page, $title)
58+
public function index($page, $title)
5959
{
6060
// ...
6161
}
6262

6363
Alternatively, the title could be accessed through the ``Request`` object::
6464

6565
use Symfony\Component\HttpFoundation\Request;
66-
67-
public function indexAction(Request $request, $page)
66+
67+
public function index(Request $request, $page)
6868
{
6969
$title = $request->attributes->get('title');
7070

0 commit comments

Comments
 (0)