8000 moved management of the locale from the Session class to the Request … · hackur/symfony@74bc699 · GitHub
[go: up one dir, main page]

Skip to content

Commit 74bc699

Browse files
committed
moved management of the locale from the Session class to the Request class
The locale management does not require sessions anymore. In the Symfony2 spirit, the locale should be part of your URLs. If this is the case (via the special _locale request attribute), Symfony will store it in the request (getLocale()). This feature is now also configurable/replaceable at will as everything is now managed by the new LocaleListener event listener. How to upgrade: The default locale configuration has been moved from session to the main configuration: Before: framework: session: default_locale: en After: framework: default_locale: en Whenever you want to get the current locale, call getLocale() on the request (was on the session before).
1 parent 8b55541 commit 74bc699

File tree

26 files changed

+264
-152
lines changed

26 files changed

+264
-152
lines changed

CHANGELOG-2.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
5959

6060
### HttpFoundation
6161

62+
* [BC BREAK] moved management of the locale from the Session class to the Request class
6263
* added a generic access to the PHP built-in filter mechanism: ParameterBag::filter()
6364
* made FileBinaryMimeTypeGuesser command configurable
6465
* added Request::getUser() and Request::getPassword()

UPGRADE-2.1.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,40 @@ UPGRADE FROM 2.0 to 2.1
33

44
* assets_base_urls and base_urls merging strategy has changed
55

6-
Unlike most configuration blocks, successive values for
7-
``assets_base_urls`` will overwrite each other instead of being merged.
8-
This behavior was chosen because developers will typically define base
9-
URL's for each environment. Given that most projects tend to inherit
10-
configurations (e.g. ``config_test.yml`` imports ``config_dev.yml``)
11-
and/or share a common base configuration (i.e. ``config.yml``), merging
12-
could yield a set of base URL's for multiple environments.
6+
Unlike most configuration blocks, successive values for
7+
``assets_base_urls`` will overwrite each other instead of being merged.
8+
This behavior was chosen because developers will typically define base
9+
URL's for each environment. Given that most projects tend to inherit
10+
configurations (e.g. ``config_test.yml`` imports ``config_dev.yml``)
11+
and/or share a common base configuration (i.e. ``config.yml``), merging
12+
could yield a set of base URL's for multiple environments.
1313

14+
* moved management of the locale from the Session class to the Request class
15+
16+
Configuring the default locale:
17+
18+
Before:
19+
20+
framework:
21+
session:
22+
default_locale: fr
23+
24+
After:
25+
26+
framework:
27+
default_locale: fr
28+
29+
Retrieving the locale from a Twig template:
30+
31+
Before: {{ app.request.session.locale }}
32+
After: {{ app.request.locale }}
33+
34+
Retrieving the locale from a PHP template:
35+
36+
Before: $view['session']->getLocale()
37+
After: $view['request']->getLocale()
38+
39+
Retrieving the locale from PHP code:
40+
41+
Before: $session->getLocale()
42+
After: $request->getLocale()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function getConfigTreeBuilder()
5151
->scalarNode('secret')->isRequired()->end()
5252
->scalarNode('ide')->defaultNull()->end()
5353
->booleanNode('test')->end()
54+
->scalarNode('default_locale')->defaultValue('en')->end()
5455
->end()
5556
;
5657

@@ -161,7 +162,6 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
161162
->canBeUnset()
162163
->children()
163164
->booleanNode('auto_start')->defaultFalse()->end()
164-
->scalarNode('default_locale')->defaultValue('en')->end()
165165
->scalarNode('storage_id')->defaultValue('session.storage.native')->end()
166166
->scalarNode('name')->end()
167167
->scalarNode('lifetime')->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public function load(array $configs, ContainerBuilder $container)
6565

6666
$container->setParameter('kernel.trust_proxy_headers', $config['trust_proxy_headers']);
6767

68+
$container->setParameter('kernel.default_locale', $config['default_locale']);
69+
6870
if (!empty($config['test'])) {
6971
$loader->load('test.xml');
7072
}
@@ -280,7 +282,6 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c
280282

281283
// session
282284
$container->getDefinition('session_listener')->addArgument($config['auto_start']);
283-
$container->setParameter('session.default_locale', $config['default_locale']);
284285

285286
// session storage
286287
$container->setAlias('session.storage', $config['storage_id' E377 ]);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\EventListener;
13+
14+
use Symfony\Component\HttpKernel\HttpKernelInterface;
15+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
16+
use Symfony\Component\Routing\RouterInterface;
17+
18+
/**
19+
* Initializes the locale based on the current request.
20+
*
21+
* @author Fabien Potencier <fabien@symfony.com>
22+
*/
23+
class LocaleListener
24+
{
25+
private $router;
26+
private $defaultLocale;
27+
28+
public function __construct($defaultLocale = 'en', RouterInterface $router = null)
29+
{
30+
$this->defaultLocale = $defaultLocale;
31+
$this->router = $router;
32+
}
33+
34+
public function onEarlyKernelRequest(GetResponseEvent $event)
35+
{
36+
$request = $event->getRequest();
37+
if ($request->hasPreviousSession()) {
38+
$request->setDefaultLocale($request->getSession()->get('_locale', $this->defaultLocale));
39+
} else {
40+
$request->setDefaultLocale($this->defaultLocale);
41+
}
42+
}
43+
44+
public function onKernelRequest(GetResponseEvent $event)
45+
{
46+
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
47+
return;
48+
}
49+
50+
$request = $event->getRequest();
51+
if ($locale = $request->attributes->get('_locale')) {
52+
$request->setLocale($locale);
53+
54+
if ($request->hasPreviousSession()) {
55+
$request->getSession()->set('_locale', $request->getLocale());
56+
}
57+
}
58+
59+
if (null !== $this->router) {
60+
$this->router->getContext()->setParameter('_locale', $request->getLocale());
61+
}
62+
}
63+
}

src/Symfony/Bundle/FrameworkBundle/EventListener/RouterListener.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,6 @@ public function onKernelRequest(GetResponseEvent $event)
8888

8989
throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
9090
}
91-
92-
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
93-
$context = $this->router->getContext();
94-
$session = $request->getSession();
95-
if ($locale = $request->attributes->get('_locale')) {
96-
if ($session) {
97-
$session->setLocale($locale);
98-
}
99-
$context->setParameter('_locale', $locale);
100-
} elseif ($session) {
101-
$context->setParameter('_locale', $session->getLocale());
102-
}
103-
}
10491
}
10592

10693
private function parametersToString(array $parameters)

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<xsd:attribute name="trust-proxy-headers" type="xsd:string" />
2626
<xsd:attribute name="ide" type="xsd:string" />
2727
<xsd:attribute name="secret" type="xsd:string" />
28+
<xsd:attribute name="default-locale" type="xsd:string" />
2829
</xsd:complexType>
2930

3031
<xsd:complexType name="form">
@@ -72,7 +73,6 @@
7273

7374
<xsd:complexType name="session">
7475
<xsd:attribute name="storage-id" type="xsd:string" />
75-
<xsd:attribute name="default-locale" type="xsd:string" />
7676
<xsd:attribute name="name" type="xsd:string" />
7777
<xsd:attribute name="lifetime" type="xsd:integer" />
7878
<xsd:attribute name="path" type="xsd:string" />

src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
<services>
1515
<service id="session" class="%session.class%">
1616
<argument type="service" id="session.storage" />
17-
<argument>%session.default_locale%</argument>
1817
</service>
1918

2019
<service id="session.storage.native" class="%session.storage.native.class%" public="false">

src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
<argument key="debug">%kernel.debug%</argument>
3838
<argument key="charset">%kernel.charset%</argument>
3939
</argument>
40-
<argument type="service" id="session" on-invalid="ignore" />
4140
</service>
4241

4342
<service id="translator" class="%translator.identity.class%">

src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<parameter key="controller_resolver.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver</parameter>
99
<parameter key="controller_name_converter.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser</parameter>
1010
<parameter key="response_listener.class">Symfony\Component\HttpKernel\EventListener\ResponseListener</parameter>
11+
<parameter key="locale_listener.class">Symfony\Bundle\FrameworkBundle\EventListener\LocaleListener</parameter>
1112
</parameters>
1213

1314
<services>
@@ -27,5 +28,12 @@
2728
<tag name="kernel.event_listener" event="kernel.response" method="onKernelResponse" />
2829
<argument>%kernel.charset%</argument>
2930
</service>
31+
32+
<service id="locale_listener" class="%locale_listener.class%">
33+
<tag name="kernel.event_listener" event="kernel.request" method="onEarlyKernelRequest" priority="255" />
34+
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="-1" />
35+
<argument>%kernel.default_locale%</argument>
36+
<argument type="service" id="router" on-invalid="ignore" />
37+
</service>
3038
</services>
3139
</container>

0 commit comments

Comments
 (0)
0