Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fixed route examples #11085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 46 additions & 57 deletions 103 components/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -302,20 +302,18 @@ other loaders that work the same way:
* :class:`Symfony\\Component\\Routing\\Loader\\PhpFileLoader`

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

// RouteProvider.php
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

$routes = new RouteCollection();
$routes->add(
'route_name',
new Route('/foo', ['_controller' => 'ExampleController'])
);
// ...
namespace Symfony\Component\Routing\Loader\Configurator;

return $routes;
return function (RoutingConfigurator $routes) {
$routes->add('route_name', '/foo')
->controller('ExampleController')
// ...
;
};

Routes as Closures
..................
Expand Down Expand Up @@ -410,7 +408,7 @@ routes with UTF-8 characters:

route1:
path: /category/{name}
defaults: { _controller: 'App\Controller\DefaultController::category' }
controller: App\Controller\DefaultController::category
options:
utf8: true

Expand All @@ -422,31 +420,26 @@ routes with UTF-8 characters:
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="route1" path="/category/{name}">
<default key="_controller">App\Controller\DefaultController::category</default>
<route id="route1" path="/category/{name}" controller="App\Controller\DefaultController::category">
<option key="utf8">true</option>
</route>
</routes>

.. code-block:: php

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

$routes = new RouteCollection();
$routes->add('route1', new Route('/category/{name}',
[
'_controller' => 'App\Controller\DefaultController::category',
],
[],
[
'utf8' => true,
]
));
// config/routes.php
namespace Symfony\Component\Routing\Loader\Configurator;

// ...
use App\Controller\DefaultController;

return $routes;
return function (RoutingConfigurator $routes) {
$routes->add('route1', '/category/{name}')
->controller([DefaultController::class, 'category'])
->options([
'utf8' => true,
])
;
};

In this route, the ``utf8`` option set to ``true`` makes Symfony consider the
``.`` requirement to match any UTF-8 characters instead of just a single
Expand All @@ -471,22 +464,22 @@ You can also include UTF-8 strings as routing requirements:
* @Route(
* "/category/{name}",
* name="route2",
* requirements={"default"="한국어"},
* defaults={"name"="한국어"},
* options={"utf8": true}
* )
*/
public function default()
public function category()
{
// ...
}

.. code-block:: yaml

route2:
path: /default/{default}
defaults: { _controller: 'App\Controller\DefaultController::default' }
requirements:
default: "한국어"
path: /category/{name}
controller: 'App\Controller\DefaultController::category'
defaults:
name: "한국어"
options:
utf8: true

Expand All @@ -498,34 +491,30 @@ You can also include UTF-8 strings as routing requirements:
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="route2" path="/default/{default}">
<default key="_controller">App\Controller\DefaultController::default</default>
<requirement key="default">한국어</requirement>
<route id="route2" path="/category/{name}" controller="App\Controller\DefaultController::category">
<default key="name">한국어</default>
<option key="utf8">true</option>
</route>
</routes>

.. code-block:: php

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

$routes = new RouteCollection();
$routes->add('route2', new Route('/default/{default}',
[
'_controller' => 'App\Controller\DefaultController::default',
],
[
'default' => '한국어',
],
[
'utf8' => true,
]
));

// ...

return $routes;
// config/routes.php
namespace Symfony\Component\Routing\Loader\Configurator;

use App\Controller\DefaultController;

return function (RoutingConfigurator $routes) {
$routes->add('route2', '/category/{name}')
->controller([DefaultController::class, 'category'])
->defaults([
'name' => '한국어',
])
->options([
'utf8' => true,
])
;
};

.. tip::

Expand Down
17 changes: 7 additions & 10 deletions 17 controller/error_pages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,22 +171,19 @@ automatically when installing Twig support):
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<import resource="@TwigBundle/Resources/config/routing/errors.xml"
prefix="/_error" />
<import resource="@TwigBundle/Resources/config/routing/errors.xml" prefix="/_error" />
</routes>

.. code-block:: php

// config/routes/dev/twig.php
use Symfony\Component\Routing\RouteCollection;
namespace Symfony\Component\Routing\Loader\Configurator;

$routes = new RouteCollection();
$routes->addCollection(
$loader->import('@TwigBundle/Resources/config/routing/errors.xml')
);
$routes->addPrefix("/_error");

return $routes;
return function (RoutingConfigurator $routes) {
$routes->import('@TwigBundle/Resources/config/routing/errors.xml')
->prefix('/_error')
;
};

With this route added, you can use URLs like these to preview the *error* page
for a given status code as HTML or for a given status code and format.
Expand Down
22 changes: 14 additions & 8 deletions 22 controller/service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ a service like: ``App\Controller\HelloController::index``:
class HelloController
{
/**
* @Route("/hello", name="hello")
* @Route("/hello", name="hello", methods={"GET"})
*/
public function index()
{
Expand All @@ -45,7 +45,8 @@ a service like: ``App\Controller\HelloController::index``:
# config/routes.yaml
hello:
path: /hello
defaults: { _controller: App\Controller\HelloController::index }
controller: App\Controller\HelloController::index
methods: GET

.. code-block:: xml

Expand All @@ -56,18 +57,23 @@ a service like: ``App\Controller\HelloController::index``:
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="hello" path="/hello">
<default key="_controller">App\Controller\HelloController::index</default>
</route>
<route id="hello" path="/hello" controller="App\Controller\HelloController::index" methods="GET" />

</routes>

.. code-block:: php

// config/routes.php
$collection->add('hello', new Route('/hello', [
'_controller' => 'App\Controller\HelloController::index',
]));
namespace Symfony\Component\Routing\Loader\Configurator;

use App\Controller\HelloController;

return function (RoutingConfigurator $routes) {
$routes->add('hello', '/hello')
->controller(['HelloController::class, 'index'])
->methods(['GET'])
;
};

.. _controller-service-invoke:

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.