|
1 | 1 | Sessions
|
2 | 2 | ========
|
3 | 3 |
|
4 |
| -Symfony provides a nice session object that you can use to store information |
5 |
| -about the user between requests. |
| 4 | +Symfony provides a session object and several utilities that you can use to |
| 5 | +store information about the user between requests. |
6 | 6 |
|
7 |
| -To see how to use the session, read :ref:`session-intro`. |
| 7 | +Configuration |
| 8 | +------------- |
| 9 | + |
| 10 | +Sessions are provided by the `HttpFoundation component`_, which is included in |
| 11 | +all Symfony applications, no matter how you installed it. Before using the |
| 12 | +sessions, check their configuration: |
| 13 | + |
| 14 | +.. configuration-block:: |
| 15 | + |
| 16 | + .. code-block:: yaml |
| 17 | +
|
| 18 | + # config/packages/framework.yaml |
| 19 | + framework: |
| 20 | + session: |
| 21 | + # enables the support of sessions in the app |
| 22 | + enabled: true |
| 23 | +
|
| 24 | + # ID of the service used for session storage |
| 25 | + handler_id: session.handler.native_file |
| 26 | +
|
| 27 | + # the directory where session metadata is stored |
| 28 | + save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%' |
| 29 | +
|
| 30 | + .. code-block:: xml |
| 31 | +
|
| 32 | + <!-- config/packages/framework.xml --> |
| 33 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 34 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 35 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 36 | + xmlns:framework="http://symfony.com/schema/dic/symfony" |
| 37 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 38 | + http://symfony.com/schema/dic/services/services-1.0.xsd |
| 39 | + http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> |
| 40 | +
|
| 41 | + <framework:config> |
| 42 | + <!-- |
| 43 | + enabled: enables the support of sessions in the app |
| 44 | + handler-id: ID of the service used for session storage |
| 45 | + save_path: the directory where session metadata is stored |
| 46 | + --> |
| 47 | + <framework:session enabled="true" |
| 48 | + handler-id="session.handler.native_file" |
| 49 | + save-path="%kernel.project_dir%/var/sessions/%kernel.environment%" /> |
| 50 | + </framework:config> |
| 51 | + </container> |
| 52 | +
|
| 53 | + .. code-block:: php |
| 54 | +
|
| 55 | + // config/packages/framework.php |
| 56 | + $container->loadFromExtension('framework', [ |
| 57 | + 'session' => [ |
| 58 | + // enables the support of sessions in the app |
| 59 | + 'enabled' => true, |
| 60 | + // ID of the service used for session storage |
| 61 | + 'handler_id' => 'session.handler.native_file', |
| 62 | + // the directory where session metadata is stored |
| 63 | + 'save_path' => '%kernel.project_dir%/var/sessions/%kernel.environment%', |
| 64 | + ], |
| 65 | + ]); |
| 66 | +
|
| 67 | +Check out the Symfony config reference to learn more about the other available |
| 68 | +:ref:`Session configuration options <config-framework-session>`. Also, if you |
| 69 | +prefer to store session metadata in the database instead of the filesystem, |
| 70 | +check out this article: :doc:`/doctrine/pdo_session_storage`. |
| 71 | + |
| 72 | +Basic Usage |
| 73 | +----------- |
| 74 | + |
| 75 | +Symfony provides a session service that is injected in your services and |
| 76 | +controllers if you type-hint an argument with |
| 77 | +:class:`Symfony\\Component\\HttpFoundation\\Session\\SessionInterface`:: |
| 78 | + |
| 79 | + use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 80 | + |
| 81 | + class SomeService |
| 82 | + { |
| 83 | + private $session; |
| 84 | + |
| 85 | + public function __construct(SessionInterface $session) |
| 86 | + { |
| 87 | + $this->session = $session; |
| 88 | + } |
| 89 | + |
| 90 | + public function someMethod() |
| 91 | + { |
| 92 | + // stores an attribute in the session for later reuse |
| 93 | + $session->set('attribute-name', 'attribute-value'); |
| 94 | + |
| 95 | + // gets an attribute by name |
| 96 | + $foo = $session->get('foo'); |
| 97 | + |
| 98 | + // uses a default value if the attribute doesn't exist |
| 99 | + $filters = $session->get('filters', []); |
| 100 | + |
| 101 | + // ... |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | +Stored attributes remain in the session for the remainder of that user's session. |
| 106 | + |
| 107 | +.. tip:: |
| 108 | + |
| 109 | + Every ``SessionInterface`` implementation is supported. If you have your |
| 110 | + own implementation, type-hint this in the argument instead. |
| 111 | + |
| 112 | +.. _session-avoid-start: |
| 113 | + |
| 114 | +Avoid Starting Sessions for Anonymous Users |
| 115 | +------------------------------------------- |
| 116 | + |
| 117 | +Sessions are automatically started whenever you read, write or even check for |
| 118 | +the existence of data in the session. This may hurt your application performance |
| 119 | +because all users will receive a session cookie. In order to prevent that, you |
| 120 | +must *completely* avoid accessing the session. |
| 121 | + |
| 122 | +For example, if your templates include some code to display the |
| 123 | +:ref:`flash messages <flash-messages>`, sessions will start even if the user |
| 124 | +is not logged in and even if you haven't created any flash messages. To avoid |
| 125 | +this behavior, add a check before trying to access the flash messages: |
| 126 | + |
| 127 | +.. code-block:: html+twig |
| 128 | + |
| 129 | + {# this check prevents starting a session when there are no flash messages #} |
| 130 | + {% if app.request.hasPreviousSession %} |
| 131 | + {% for message in app.flashes('notice') %} |
| 132 | + <div class="flash-notice"> |
| 133 | + {{ message }} |
| 134 | + </div> |
| 135 | + {% endfor %} |
| 136 | + {% endif %} |
8 | 137 |
|
9 | 138 | More about Sessions
|
10 | 139 | -------------------
|
11 | 140 |
|
12 | 141 | .. toctree::
|
13 | 142 | :maxdepth: 1
|
14 | 143 |
|
15 |
| - session/sessions_directory |
16 |
| - session/avoid_session_start |
| 144 | + /doctrine/pdo_session_storage |
17 | 145 | session/locale_sticky_session
|
18 | 146 | session/php_bridge
|
19 | 147 | session/proxy_examples
|
20 | 148 |
|
21 |
| -* :doc:`/doctrine/pdo_session_storage` |
| 149 | +.. _`HttpFoundation component`: https://symfony.com/components/HttpFoundation |
0 commit comments