Symfony News

New in Symfony 3.2: Routing Improvements

Added support for URL fragments

Contributed by
Rhodri Pugh
in #12979.

The fragment identifier is the optional last part of a URL that starts with a # character and it's used to identify a portion of a document. This URL element is increasingly popular because some applications use it as a navigation mechanism. For that reason, Symfony 3.2 allows to define the fragment when generating a URL thanks to a new reserved routing property called _fragment:

1
2
3
4
5
// generating a regular URL (/settings)
$this->get('router')->generate('user_settings');

// generating a URL with a fragment (/settings#password)
$this->get('router')->generate('user_settings', ['_fragment' => 'password']);

This _fragment option can also be used when defining the route in any of the formats supported by Symfony:

1
2
3
4
/**
 * @Route("/settings", defaults={"_fragment" = "password"}, name="user_settings")
 */
public function settingsAction() { ... }

Added support for array values in XML routes

Contributed by
Christian Flothmann
in #11394.

XML is not one of the most popular formats to define routes in Symfony applications. In addition to its verbosity, it lacks some features from other formats, such as using arrays to define the default routing values:

1
2
3
4
5
6
7
8
<routes>
    <route id="blog" path="/blog/{page}">
        <default key="_controller">AppBundle:Blog:index</default>
        <!-- you can't define the type of the 'page' property and you can't
             use an array as the value of a '<default>' element -->
        <default key="page">1</default>
    </route>
</routes>

In Symfony 3.2 we decided to improve the XmlFileLoader class of the Routing component to allow defining the variable type of any <default> element:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<routes>
    <route id="blog" path="/blog/{page}">
        <default key="_controller">
            <string>AppBundle:Blog:index</string>
        </default>
        <default key="page">
            <int>1</int>
        </default>
    </route>
</routes>

Now you can also use arrays as the value of any <default> element (using <list> for scalar arrays and <map> for associative arrays):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<routes>
    <route id="blog" path="/blog/{page}">
        <default key="_controller">
            <string>AppBundle:Blog:index</string>
        </default>
        <default key="page">
            <int>1</int>
        </default>
        <default key="values">
            <map>
                <bool key="public">true</bool>
                <int key="page">1</int>
                <float key="price">3.5</float>
                <string key="title">foo</string>
            </map>
        </default>
    </route>
</routes>

Be trained by Symfony experts - 2016-07-06 London - 2016-07-11 Paris - 2016-07-18 Paris


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook