Skip to content

Updated routing articles to Symfony 4 #8657

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

Merged
merged 2 commits into from
Nov 17, 2017
Merged
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
13 changes: 8 additions & 5 deletions routing/conditions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,37 @@ define arbitrary matching logic, use the ``conditions`` routing option:

.. code-block:: yaml

# config/routes.yaml
contact:
path: /contact
defaults: { _controller: AcmeDemoBundle:Main:contact }
condition: "context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i'"
path: /contact
controller: 'App\Controller\DefaultController::contact'
condition: "context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i'"

.. code-block:: xml

<!-- config/routes.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="contact" path="/contact">
<default key="_controller">AcmeDemoBundle:Main:contact</default>
<default key="_controller">App\Controller\DefaultController::contact</default>
<condition>context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i'</condition>
</route>
</routes>

.. code-block:: php

// config/routes.php
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

$collection = new RouteCollection();
$collection->add('contact', new Route(
'/contact', array(
'_controller' => 'AcmeDemoBundle:Main:contact',
'_controller' => 'App\Controller\DefaultController::contact',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's minor, and not worth delaying things because it's such a not important format, but in PHP, using the [DefaultController::class, 'contact'] is friendlier.

),
array(),
array(),
Expand Down
28 changes: 15 additions & 13 deletions routing/custom_route_loader.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ and therefore have two important methods:
:method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::supports`
and :method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::load`.

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

.. code-block:: yaml

# config/routes.yaml
app:
resource: '@AppBundle/Controller/'
type: annotation
controllers:
resource: ../src/Controller/
type: annotation

When the main loader parses this, it tries all registered delegate loaders and calls
their :method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::supports`
method with the given resource (``@AppBundle/Controller/``)
method with the given resource (``../src/Controller/``)
and type (``annotation``) as arguments. When one of the loader returns ``true``,
its :method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::load` method
will be called, which should return a :class:`Symfony\\Component\\Routing\\RouteCollection`
Expand All @@ -56,7 +56,7 @@ containing :class:`Symfony\\Component\\Routing\\Route` objects.
.. note::

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

Creating a custom Loader
Expand Down Expand Up @@ -97,7 +97,7 @@ you do. The resource name itself is not actually used in the example::
// prepare a new route
$path = '/extra/{parameter}';
$defaults = array(
'_controller' => 'AppBundle:Extra:extra',
'_controller' => 'App\Controller\ExtraController::extra',
);
$requirements = array(
'parameter' => '\d+',
Expand All @@ -120,8 +120,7 @@ you do. The resource name itself is not actually used in the example::
}

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

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

class ExtraController extends Controller
{
public function extraAction($parameter)
public function extra($parameter)
{
return new Response($parameter);
}
Expand All @@ -152,6 +151,7 @@ Now define a service for the ``ExtraLoader``:

.. code-block:: xml

<!-- config/services.xml -->
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -169,6 +169,7 @@ Now define a service for the ``ExtraLoader``:

.. code-block:: php

// config/services.php
use App\Routing\ExtraLoader;

$container
Expand Down Expand Up @@ -198,6 +199,7 @@ What remains to do is adding a few lines to the routing configuration:

.. code-block:: xml

<!-- config/routes.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -217,10 +219,10 @@ What remains to do is adding a few lines to the routing configuration:

return $collection;

The important part here is the ``type`` key. Its value should be "extra" as
The important part here is the ``type`` key. Its value should be ``extra`` as
this is the type which the ``ExtraLoader`` supports and this will make sure
its ``load()`` method gets called. The ``resource`` key is insignificant
for the ``ExtraLoader``, so it is set to ".".
for the ``ExtraLoader``, so it is set to ``.`` (a single dot).

.. note::

Expand Down Expand Up @@ -256,7 +258,7 @@ configuration file - you can call the
{
$collection = new RouteCollection();

$resource = '@AppBundle/Resources/config/import_routing.yml';
$resource = '@ThirdPartyBundle/Resources/config/routing.yaml';
$type = 'yaml';

$importedRoutes = $this->import($resource, $type);
Expand Down
39 changes: 17 additions & 22 deletions routing/external_resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ This can be done by "importing" directories into the routing configuration:
.. code-block:: yaml

# config/routes.yaml
app:
resource: '@AppBundle/Controller/'
type: annotation # required to enable the Annotation reader for this resource
controllers:
resource: ../src/Controller/
type: annotation

.. code-block:: xml

Expand All @@ -27,8 +27,7 @@ This can be done by "importing" directories into the routing configuration:
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<!-- the type is required to enable the annotation reader for this resource -->
<import resource="@AppBundle/Controller/" type="annotation"/>
<import resource="../src/Controller/" type="annotation"/>
</routes>

.. code-block:: php
Expand All @@ -38,35 +37,31 @@ This can be done by "importing" directories into the routing configuration:

$collection = new RouteCollection();
$collection->addCollection(
// second argument is the type, which is required to enable
// the annotation reader for this resource
$loader->import("@AppBundle/Controller/", "annotation")
$loader->import("../src/Controller/", "annotation")
);

return $collection;

.. note::

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

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

.. note::

You can also include other routing configuration files, this is often
used to import the routing of third party bundles:
You can also include other routing configuration files:

.. configuration-block::

.. code-block:: yaml

# config/routes.yaml
app:
resource: '@AcmeOtherBundle/Resources/config/routing.yml'
resource: '@ThirdPartyBundle/Resources/config/routing.yaml'

.. code-block:: xml

Expand All @@ -77,7 +72,7 @@ in that directory are parsed and put into the routing.
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<import resource="@AcmeOtherBundle/Resources/config/routing.xml" />
<import resource="@ThirdPartyBundle/Resources/config/routing.xml" />
</routes>

.. code-block:: php
Expand All @@ -87,7 +82,7 @@ in that directory are parsed and put into the routing.

$collection = new RouteCollection();
$collection->addCollection(
$loader->import("@AcmeOtherBundle/Resources/config/routing.php")
$loader->import("@ThirdPartyBundle/Resources/config/routing.php")
);

return $collection;
Expand All @@ -96,16 +91,16 @@ Prefixing Imported Routes
~~~~~~~~~~~~~~~~~~~~~~~~~

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

.. configuration-block::

.. code-block:: yaml

# config/routes.yaml
app:
resource: '@AppBundle/Controller/'
controllers:
resource: '../src/Controller/'
type: annotation
prefix: /site

Expand All @@ -119,7 +114,7 @@ suppose you want to prefix all routes in the AppBundle with ``/site`` (e.g.
http://symfony.com/schema/routing/routing-1.0.xsd">

<import
resource="@AppBundle/Controller/"
resource="../src/Controller/"
type="annotation"
prefix="/site" />
</routes>
Expand All @@ -129,7 +124,7 @@ suppose you want to prefix all routes in the AppBundle with ``/site`` (e.g.
// config/routes.php
use Symfony\Component\Routing\RouteCollection;

$app = $loader->import('@AppBundle/Controller/', 'annotation');
$app = $loader->import('../src/Controller/', 'annotation');
$app->addPrefix('/site');

$collection = new RouteCollection();
Expand Down
18 changes: 9 additions & 9 deletions routing/extra_information.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ to your controller, and as attributes of the ``Request`` object:

# config/routes.yaml
blog:
path: /blog/{page}
path: /blog/{page}
controller: App\Controller\BlogController::index
defaults:
_controller: AppBundle:Blog:index
page: 1
title: "Hello world!"
page: 1
title: "Hello world!"

.. code-block:: xml

Expand All @@ -31,7 +31,7 @@ to your controller, and as attributes of the ``Request`` object:
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="blog" path="/blog/{page}">
<default key="_controller">AppBundle:Blog:index</default>
<default key="_controller">App\Controller\BlogController::index</default>
<default key="page">1</default>
<default key="title">Hello world!</default>
</route>
Expand All @@ -45,7 +45,7 @@ to your controller, and as attributes of the ``Request`` object:

$collection = new RouteCollection();
$collection->add('blog', new Route('/blog/{page}', array(
'_controller' => 'AppBundle:Blog:index',
'_controller' => 'App\Controller\BlogController::index',
'page' => 1,
'title' => 'Hello world!',
)));
Expand All @@ -55,16 +55,16 @@ to your controller, and as attributes of the ``Request`` object:
Now, you can access this extra parameter in your controller, as an argument
to the controller method::

public function indexAction($page, $title)
public function index($page, $title)
{
// ...
}

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

use Symfony\Component\HttpFoundation\Request;
public function indexAction(Request $request, $page)

public function index(Request $request, $page)
{
$title = $request->attributes->get('title');

Expand Down
Loading