Added support for URL fragments
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
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>
The integer and boolean XML elements have been renamed to int and bool after the initial pull request was merged (see https://github.com/symfony/symfony/pull/19157).
Thanks for contribution :)
@Christian thanks for your contribution and thanks for noticing the error in the article. It's now updated.
Being able to generate the fragment part of an url with the UrlGenerator seems a nice addition. It always felt a bit weird to generate the url, and then concatenate the '#foo' part.
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!
Does the _fragment parameter also work for the "redirect" method?