Skip to content

Commit 34033db

Browse files
author
Jules Pietri
committed
used RoutingConfigurator in examples
1 parent d245b45 commit 34033db

21 files changed

+516
-501
lines changed

components/routing.rst

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -302,20 +302,18 @@ other loaders that work the same way:
302302
* :class:`Symfony\\Component\\Routing\\Loader\\PhpFileLoader`
303303

304304
If you use the :class:`Symfony\\Component\\Routing\\Loader\\PhpFileLoader` you
305-
have to provide the name of a PHP file which returns a :class:`Symfony\\Component\\Routing\\RouteCollection`::
305+
have to provide the name of a PHP file which returns a callable handling a :class:`Symfony\\Component\\Routing\\Loader\\Configurator\\RoutingConfigurator`.
306+
This class allows to chain imports, collections or simple route definition calls::
306307

307308
// RouteProvider.php
308-
use Symfony\Component\Routing\RouteCollection;
309-
use Symfony\Component\Routing\Route;
310-
311-
$routes = new RouteCollection();
312-
$routes->add(
313-
'route_name',
314-
new Route('/foo', ['_controller' => 'ExampleController'])
315-
);
316-
// ...
309+
namespace Symfony\Component\Routing\Loader\Configurator;
317310

318-
return $routes;
311+
return function (RoutingConfigurator $routes) {
312+
$routes->add('route_name', '/foo')
313+
->controller('ExampleController')
314+
// ...
315+
;
316+
};
319317

320318
Routes as Closures
321319
..................
@@ -410,7 +408,7 @@ routes with UTF-8 characters:
410408
411409
route1:
412410
path: /category/{name}
413-
defaults: { _controller: 'App\Controller\DefaultController::category' }
411+
controller: App\Controller\DefaultController::category
414412
options:
415413
utf8: true
416414
@@ -429,23 +427,19 @@ routes with UTF-8 characters:
429427
430428
.. code-block:: php
431429
432-
use Symfony\Component\Routing\RouteCollection;
433-
use Symfony\Component\Routing\Route;
434-
435-
$routes = new RouteCollection();
436-
$routes->add('route1', new Route('/category/{name}',
437-
[
438-
'_controller' => 'App\Controller\DefaultController::category',
439-
],
440-
[],
441-
[
442-
'utf8' => true,
443-
]
444-
));
430+
// config/routes.php
431+
namespace Symfony\Component\Routing\Loader\Configurator;
445432
446-
// ...
433+
use App\Controller\DefaultController;
447434
448-
return $routes;
435+
return function (RoutingConfigurator $routes) {
436+
$routes->add('route1', '/category/{name}')
437+
->controller([DefaultController::class, 'category'])
438+
->options([
439+
'utf8' => true,
440+
])
441+
;
442+
};
449443
450444
In this route, the ``utf8`` option set to ``true`` makes Symfony consider the
451445
``.`` requirement to match any UTF-8 characters instead of just a single
@@ -470,22 +464,22 @@ You can also include UTF-8 strings as routing requirements:
470464
* @Route(
471465
* "/category/{name}",
472466
* name="route2",
473-
* requirements={"default"="한국어"},
467+
* defaults={"name"="한국어"},
474468
* options={"utf8": true}
475469
* )
476470
*/
477-
public function default()
471+
public function category()
478472
{
479473
// ...
480474
}
481475
482476
.. code-block:: yaml
483477
484478
route2:
485-
path: /default/{default}
486-
defaults: { _controller: 'App\Controller\DefaultController::default' }
487-
requirements:
488-
default: "한국어"
479+
path: /category/{name}
480+
controller: 'App\Controller\DefaultController::category'
481+
defaults:
482+
name: "한국어"
489483
options:
490484
utf8: true
491485
@@ -497,34 +491,30 @@ You can also include UTF-8 strings as routing requirements:
497491
xsi:schemaLocation="http://symfony.com/schema/routing
498492
http://symfony.com/schema/routing/routing-1.0.xsd">
499493
500-
<route id="route2" path="/default/{default}"
501-
controller="App\Controller\DefaultController::default">
502-
<requirement key="default">한국어</requirement>
494+
<route id="route2" path="/category/{name}" controller="App\Controller\DefaultController::category">
495+
<default key="name">한국어</default>
503496
<option key="utf8">true</option>
504497
</route>
505498
</routes>
506499
507500
.. code-block:: php
508501
509-
use Symfony\Component\Routing\RouteCollection;
510-
use Symfony\Component\Routing\Route;
511-
512-
$routes = new RouteCollection();
513-
$routes->add('route2', new Route('/default/{default}',
514-
[
515-
'_controller' => 'App\Controller\DefaultController::default',
516-
],
517-
[
518-
'default' => '한국어',
519-
],
520-
[
521-
'utf8' => true,
522-
]
523-
));
524-
525-
// ...
526-
527-
return $routes;
502+
// config/routes.php
503+
namespace Symfony\Component\Routing\Loader\Configurator;
504+
505+
use App\Controller\DefaultController;
506+
507+
return function (RoutingConfigurator $routes) {
508+
$routes->add('route2', '/category/{name}')
509+
->controller([DefaultController::class, 'category'])
510+
->defaults([
511+
'name' => '한국어',
512+
])
513+
->options([
514+
'utf8' => true,
515+
])
516+
;
517+
};
528518
529519
.. tip::
530520

controller/error_pages.rst

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,22 +171,19 @@ automatically when installing Twig support):
171171
xsi:schemaLocation="http://symfony.com/schema/routing
172172
http://symfony.com/schema/routing/routing-1.0.xsd">
173173
174-
<import resource="@TwigBundle/Resources/config/routing/errors.xml"
175-
prefix="/_error" />
174+
<import resource="@TwigBundle/Resources/config/routing/errors.xml" prefix="/_error" />
176175
</routes>
177176
178177
.. code-block:: php
179178
180179
// config/routes/dev/twig.php
181-
use Symfony\Component\Routing\RouteCollection;
180+
namespace Symfony\Component\Routing\Loader\Configurator;
182181
183-
$routes = new RouteCollection();
184-
$routes->addCollection(
185-
$loader->import('@TwigBundle/Resources/config/routing/errors.xml')
186-
);
187-
$routes->addPrefix("/_error");
188-
189-
return $routes;
182+
return function (RoutingConfigurator $routes) {
183+
$routes->import('@TwigBundle/Resources/config/routing/errors.xml')
184+
->prefix('/_error')
185+
;
186+
};
190187
191188
With this route added, you can use URLs like these to preview the *error* page
192189
for a given status code as HTML or for a given status code and format.

controller/service.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,16 @@ a service like: ``App\Controller\HelloController::index``:
6464
.. code-block:: php
6565
6666
// config/routes.php
67-
$collection->add('hello', new Route('/hello', [
68-
'_controller' => 'App\Controller\HelloController::index',
69-
], [], [], '', [], ['GET']));
67+
namespace Symfony\Component\Routing\Loader\Configurator;
68+
69+
use App\Controller\HelloController;
70+
71+
return function (RoutingConfigurator $routes) {
72+
$routes->add('hello', '/hello')
73+
->controller(['HelloController::class, 'index'])
74+
->methods(['GET'])
75+
;
76+
};
7077
7178
.. _controller-service-invoke:
7279

0 commit comments

Comments
 (0)