forked from symfony/symfony-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conflicts: components/config/definition.rst
- Loading branch information
Showing
12 changed files
with
325 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ Sessions | |
:maxdepth: 2 | ||
|
||
proxy_examples | ||
locale_sticky_session | ||
sessions_directory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
.. index:: | ||
single: Sessions, saving locale | ||
|
||
Making the Locale "Sticky" during a User's Session | ||
================================================== | ||
|
||
Prior to Symfony 2.1, the locale was stored in a session called ``_locale``. | ||
Since 2.1, it is stored in the Request, which means that it's not "sticky" | ||
during a user's request. In this article, you'll learn how to make the locale | ||
of a user "sticky" so that once it's set, that same locale will be used for | ||
every subsequent request. | ||
|
||
Creating LocaleListener | ||
----------------------- | ||
|
||
To simulate that the locale is stored in a session, you need to create and | ||
register a :doc:`new event listener</cookbook/service_container/event_listener>`. | ||
The listener will look something like this. Typically, ``_locale`` is used | ||
as a routing parameter to signify the locale, though it doesn't really matter | ||
how you determine the desired locale from the request:: | ||
|
||
// src/Acme/LocaleBundle/EventListener/LocaleListener.php | ||
namespace Acme\LocaleBundle\EventListener; | ||
|
||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
class LocaleListener implements EventSubscriberInterface | ||
{ | ||
private $defaultLocale; | ||
|
||
public function __construct($defaultLocale = 'en') | ||
{ | ||
$this->defaultLocale = $defaultLocale; | ||
} | ||
|
||
public function onKernelRequest(GetResponseEvent $event) | ||
{ | ||
$request = $event->getRequest(); | ||
if (!$request->hasPreviousSession()) { | ||
return; | ||
} | ||
|
||
// try to see if the locale has been set as a _locale routing parameter | ||
if ($locale = $request->attributes->get('_locale')) { | ||
$request->getSession()->set('_locale', $locale); | ||
} else { | ||
// if no explicit locale has been set on this request, use one from the session | ||
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale)); | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
// must be registered before the default Locale listener | ||
KernelEvents::REQUEST => array(array('onKernelRequest', 17)), | ||
); | ||
} | ||
} | ||
|
||
Then register the listener: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
services: | ||
acme_locale.locale_listener: | ||
class: Acme\LocaleBundle\EventListener\LocaleListener | ||
arguments: ["%kernel.default_locale%"] | ||
tags: | ||
- { name: kernel.event_subscriber } | ||
.. code-block:: xml | ||
<service id="acme_locale.locale_listener" | ||
class="Acme\LocaleBundle\EventListener\LocaleListener"> | ||
<argument>%kernel.default_locale%</argument> | ||
<tag name="kernel.event_subscriber" /> | ||
</service> | ||
.. code-block:: php | ||
use Symfony\Component\DependencyInjection\Definition; | ||
$container | ||
->setDefinition('acme_locale.locale_listener', new Definition( | ||
'Acme\LocaleBundle\EventListener\LocaleListener', | ||
array('%kernel.default_locale%') | ||
)) | ||
->addTag('kernel.event_subscriber') | ||
; | ||
That's it! Now celebrate by changing the user's locale and seeing that it's | ||
sticky throughout the request. Remember, to get the user's locale, always | ||
use the :method:`Request::getLocale<Symfony\\Component\\HttpFoundation\\Request::getLocale>` | ||
method:: | ||
|
||
// from a controller... | ||
$locale = $this->getRequest()->getLocale(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
.. index:: | ||
single: Sessions, sessions directory | ||
|
||
Configuring the Directory where Sessions Files are Saved | ||
======================================================== | ||
|
||
By default, Symfony stores the session data in the cache directory. This | ||
means that when you clear the cache, any current sessions will also be | ||
deleted. | ||
|
||
Using a different directory to save session data is one method to ensure | ||
that your current sessions aren't lost when you clear Symfony's cache. | ||
|
||
.. tip:: | ||
|
||
Using a different session save handler is an excellent (yet more complex) | ||
method of session management available within Symfony. See | ||
:doc:`/components/http_foundation/session_configuration` for a | ||
discussion of session save handlers. There is also an entry in the cookbook | ||
about storing sessions in the :doc:`database</cookbook/configuration/pdo_session_storage>`. | ||
|
||
To change the directory in which Symfony saves session data, you only need | ||
change the framework configuration. In this example, you will change the | ||
session directory to ``app/sessions``: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
# app/config/config.yml | ||
framework: | ||
session: | ||
save_path: "%kernel.root_dir%/sessions" | ||
.. code-block:: xml | ||
<!-- app/config/config.xml --> | ||
<framework:config> | ||
<framework:session save-path="%kernel.root_dir%/sessions" /> | ||
</framework:config> | ||
.. code-block:: php | ||
// app/config/config.php | ||
$container->loadFromExtension('framework', array( | ||
'session' => array('save-path' => "%kernel.root_dir%/sessions"), | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.