New in Symfony 3.2: Routing Improvements
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
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>
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Wouldn't using '#' as the routing property been more intuitive to use? Or would this 'special' character cause trouble somewhere in the code?
Anyway, nice feature. Thanks!