Skip to content

Commit 74ed0f2

Browse files
committed
Merge branch '2.1' into 2.2
Conflicts: components/config/definition.rst
2 parents c10c405 + 459b1a1 commit 74ed0f2

File tree

12 files changed

+325
-15
lines changed

12 files changed

+325
-15
lines changed

book/http_fundamentals.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ by adding an entry for ``/contact`` to your routing configuration file:
448448
449449
.. note::
450450

451-
This example uses :doc:`YAML</components/yaml/introduction>` to define the routing
451+
This example uses :doc:`YAML</components/yaml/format>` to define the routing
452452
configuration. Routing configuration can also be written in other formats
453453
such as XML or PHP.
454454

@@ -458,6 +458,8 @@ the ``AcmeDemoBundle:Main:contact`` string is a short syntax that points to a
458458
specific PHP method ``contactAction`` inside a class called ``MainController``::
459459

460460
// src/Acme/DemoBundle/Controller/MainController.php
461+
namespace Acme\DemoBundle\Controller;
462+
461463
use Symfony\Component\HttpFoundation\Response;
462464

463465
class MainController

components/config/definition.rst

+21-2
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ node definition. Node type are available for:
9999

100100
* scalar
101101
* boolean
102-
* array
103-
* enum (new in 2.1)
104102
* integer (new in 2.2)
105103
* float (new in 2.2)
104+
* enum (new in 2.1)
105+
* array
106106
* variable (no validation)
107107

108108
and are created with ``node($name, $type)`` or their associated shortcut
@@ -133,6 +133,25 @@ allowing to validate the value::
133133
->end()
134134
;
135135

136+
Enum nodes
137+
~~~~~~~~~~
138+
139+
.. versionadded:: 2.1
140+
The enum node is new in Symfony 2.1
141+
142+
Enum nodes provide a constraint to match the given input against a set of
143+
values::
144+
145+
$rootNode
146+
->children()
147+
->enumNode('gender')
148+
->values(array('male', 'female'))
149+
->end()
150+
->end()
151+
;
152+
153+
This will restrict the ``gender`` option to be either ``male`` or ``female``.
154+
136155
Array nodes
137156
~~~~~~~~~~~
138157

cookbook/map.rst.inc

+3
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
* :doc:`/cookbook/request/index`
105105

106106
* :doc:`/cookbook/request/mime_type`
107+
* (session) :doc:`/cookbook/session/locale_sticky_session`
107108

108109
* :doc:`/cookbook/routing/index`
109110

@@ -137,6 +138,8 @@
137138
* :doc:`/cookbook/session/index`
138139

139140
* :doc:`/cookbook/session/proxy_examples`
141+
* :doc:`/cookbook/session/locale_sticky_session`
142+
* :doc:`/cookbook/session/sessions_directory`
140143

141144
* **symfony1**
142145

cookbook/routing/custom_route_loader.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type you want. The resource name itself is not actually used in the example::
6666
namespace Acme\DemoBundle\Routing;
6767

6868
use Symfony\Component\Config\Loader\LoaderInterface;
69-
use Symfony\Component\Config\Loader\LoaderResolver;
69+
use Symfony\Component\Config\Loader\LoaderResolverInterface;
7070
use Symfony\Component\Routing\Route;
7171
use Symfony\Component\Routing\RouteCollection;
7272

@@ -110,7 +110,7 @@ type you want. The resource name itself is not actually used in the example::
110110
// and if you do, using the Loader base class is easier (see below)
111111
}
112112

113-
public function setResolver(LoaderResolver $resolver)
113+
public function setResolver(LoaderResolverInterface $resolver)
114114
{
115115
// same as above
116116
}

cookbook/session/index.rst

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ Sessions
55
:maxdepth: 2
66

77
proxy_examples
8+
locale_sticky_session
9+
sessions_directory
+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
.. index::
2+
single: Sessions, saving locale
3+
4+
Making the Locale "Sticky" during a User's Session
5+
==================================================
6+
7+
Prior to Symfony 2.1, the locale was stored in a session called ``_locale``.
8+
Since 2.1, it is stored in the Request, which means that it's not "sticky"
9+
during a user's request. In this article, you'll learn how to make the locale
10+
of a user "sticky" so that once it's set, that same locale will be used for
11+
every subsequent request.
12+
13+
Creating LocaleListener
14+
-----------------------
15+
16+
To simulate that the locale is stored in a session, you need to create and
17+
register a :doc:`new event listener</cookbook/service_container/event_listener>`.
18+
The listener will look something like this. Typically, ``_locale`` is used
19+
as a routing parameter to signify the locale, though it doesn't really matter
20+
how you determine the desired locale from the request::
21+
22+
// src/Acme/LocaleBundle/EventListener/LocaleListener.php
23+
namespace Acme\LocaleBundle\EventListener;
24+
25+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
26+
use Symfony\Component\HttpKernel\KernelEvents;
27+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
28+
29+
class LocaleListener implements EventSubscriberInterface
30+
{
31+
private $defaultLocale;
32+
33+
public function __construct($defaultLocale = 'en')
34+
{
35+
$this->defaultLocale = $defaultLocale;
36+
}
37+
38+
public function onKernelRequest(GetResponseEvent $event)
39+
{
40+
$request = $event->getRequest();
41+
if (!$request->hasPreviousSession()) {
42+
return;
43+
}
44+
45+
// try to see if the locale has been set as a _locale routing parameter
46+
if ($locale = $request->attributes->get('_locale')) {
47+
$request->getSession()->set('_locale', $locale);
48+
} else {
49+
// if no explicit locale has been set on this request, use one from the session
50+
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
51+
}
52+
}
53+
54+
public static function getSubscribedEvents()
55+
{
56+
return array(
57+
// must be registered before the default Locale listener
58+
KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
59+
);
60+
}
61+
}
62+
63+
Then register the listener:
64+
65+
.. configuration-block::
66+
67+
.. code-block:: yaml
68+
69+
services:
70+
acme_locale.locale_listener:
71+
class: Acme\LocaleBundle\EventListener\LocaleListener
72+
arguments: ["%kernel.default_locale%"]
73+
tags:
74+
- { name: kernel.event_subscriber }
75+
76+
.. code-block:: xml
77+
78+
<service id="acme_locale.locale_listener"
79+
class="Acme\LocaleBundle\EventListener\LocaleListener">
80+
<argument>%kernel.default_locale%</argument>
81+
82+
<tag name="kernel.event_subscriber" />
83+
</service>
84+
85+
.. code-block:: php
86+
87+
use Symfony\Component\DependencyInjection\Definition;
88+
89+
$container
90+
->setDefinition('acme_locale.locale_listener', new Definition(
91+
'Acme\LocaleBundle\EventListener\LocaleListener',
92+
array('%kernel.default_locale%')
93+
))
94+
->addTag('kernel.event_subscriber')
95+
;
96+
97+
That's it! Now celebrate by changing the user's locale and seeing that it's
98+
sticky throughout the request. Remember, to get the user's locale, always
99+
use the :method:`Request::getLocale<Symfony\\Component\\HttpFoundation\\Request::getLocale>`
100+
method::
101+
102+
// from a controller...
103+
$locale = $this->getRequest()->getLocale();
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.. index::
2+
single: Sessions, sessions directory
3+
4+
Configuring the Directory where Sessions Files are Saved
5+
========================================================
6+
7+
By default, Symfony stores the session data in the cache directory. This
8+
means that when you clear the cache, any current sessions will also be
9+
deleted.
10+
11+
Using a different directory to save session data is one method to ensure
12+
that your current sessions aren't lost when you clear Symfony's cache.
13+
14+
.. tip::
15+
16+
Using a different session save handler is an excellent (yet more complex)
17+
method of session management available within Symfony. See
18+
:doc:`/components/http_foundation/session_configuration` for a
19+
discussion of session save handlers. There is also an entry in the cookbook
20+
about storing sessions in the :doc:`database</cookbook/configuration/pdo_session_storage>`.
21+
22+
To change the directory in which Symfony saves session data, you only need
23+
change the framework configuration. In this example, you will change the
24+
session directory to ``app/sessions``:
25+
26+
.. configuration-block::
27+
28+
.. code-block:: yaml
29+
30+
# app/config/config.yml
31+
framework:
32+
session:
33+
save_path: "%kernel.root_dir%/sessions"
34+
35+
.. code-block:: xml
36+
37+
<!-- app/config/config.xml -->
38+
<framework:config>
39+
<framework:session save-path="%kernel.root_dir%/sessions" />
40+
</framework:config>
41+
42+
.. code-block:: php
43+
44+
// app/config/config.php
45+
$container->loadFromExtension('framework', array(
46+
'session' => array('save-path' => "%kernel.root_dir%/sessions"),
47+
));

quick_tour/the_big_picture.rst

+8-5
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,9 @@ is the developer's best friend.
419419
.. image:: /images/quick_tour/web_debug_toolbar.png
420420
:align: center
421421

422-
But what you see initially is only the tip of the iceberg; click on the weird
423-
hexadecimal number to reveal yet another very useful Symfony2 debugging tool:
424-
the profiler.
422+
But what you see initially is only the tip of the iceberg; click on the long
423+
hexadecimal number (the session token) to reveal yet another very useful
424+
Symfony2 debugging tool: the profiler.
425425

426426
.. image:: /images/quick_tour/profiler.png
427427
:align: center
@@ -433,7 +433,10 @@ the profiler.
433433

434434
Of course, you won't want to show these tools when you deploy your application
435435
to production. That's why you will find another front controller in the
436-
``web/`` directory (``app.php``), which is optimized for the production environment:
436+
``web/`` directory (``app.php``), which is optimized for the production environment.
437+
The ``AcmeDemoBundle`` is normally only available in the dev environment (see
438+
the note below), but if you were to add it to the production environment, you
439+
could go here:
437440

438441
.. code-block:: text
439442
@@ -446,7 +449,7 @@ And if you use Apache with ``mod_rewrite`` enabled, you can even omit the
446449
447450
http://localhost/demo/hello/Fabien
448451
449-
Last but not least, on the production servers, you should point your web root
452+
Last but not least, on production servers, you should point your web root
450453
directory to the ``web/`` directory to secure your installation and have an
451454
even better looking URL:
452455

reference/configuration/framework.rst

+5-3
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,11 @@ save_path
254254
**type**: ``string`` **default**: ``%kernel.cache.dir%/sessions``
255255

256256
This determines the argument to be passed to the save handler. If you choose
257-
the default file handler, this is the path where the files are created. You can
258-
also set this value to the ``save_path`` of your ``php.ini`` by setting the
259-
value to ``null``:
257+
the default file handler, this is the path where the session files are created.
258+
For more information, see :doc:`/cookbook/session/sessions_directory`.
259+
260+
You can also set this value to the ``save_path`` of your ``php.ini`` by setting
261+
the value to ``null``:
260262

261263
.. configuration-block::
262264

0 commit comments

Comments
 (0)