Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
7a176af4
Commit
7a176af4
authored
Mar 09, 2012
by
Jérôme Tamarelle
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cookbook recipe on how to use PdoSessionStorage
parent
eaa9500f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
0 deletions
+61
-0
doc/cookbook/index.rst
doc/cookbook/index.rst
+3
-0
doc/cookbook/session_storage.rst
doc/cookbook/session_storage.rst
+58
-0
No files found.
doc/cookbook/index.rst
View file @
7a176af4
...
...
@@ -9,6 +9,7 @@ The cookbook section contains recipes for solving specific problems.
json_request_body
translating_validation_messages
session_storage
Recipes
-------
...
...
@@ -18,3 +19,5 @@ Recipes
the request body.
* :doc:`Translating Validation Messages<translating_validation_messages>`.
* :doc:`How to use PdoSessionStorage to store sessions in the database <session_storage>`.
doc/cookbook/session_storage.rst
0 → 100644
View file @
7a176af4
How to use PdoSessionStorage to store sessions in the database
==============================================================
By default, the :doc:`SessionServiceProvider <providers/session>` write sessions
informations to file with the NativeFileSessionStorage of Symfony2. Most medium
to large websites use a database to store the session values instead of files,
because databases are easier to use and scale in a multi-webserver environment.
Symfony2 has multiple session storage solutions. For database, its called
`PdoSessionStorage <http://api.symfony.com/master/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.html>
To use it, you need to replace the ``session.storage`` service in your application.
Example
-------
use Symfony\Component\HttpFoundation\Session\Storage\PdoSessionStorage;
$app->register(new Silex\Provider\SessionServiceProvider());
$app['pdo.dsn'] = 'mysql:dbname=mydatabase';
$app['pdo.user'] = 'myuser';
$app['pdo.password'] = 'mypassword';
$app['pdo.db_options'] = array(
'db_table' => 'session',
'db_id_col' => 'session_id',
'db_data_col' => 'session_value',
'db_time_col' => 'session_time',
);
$app['pdo'] = $app->share(function () use ($app) {
return new PDO(
$app['pdo.dsn'],
$app['pdo.user'],
$app['pdo.password']
);
});
$app['session.storage'] = $app->share(function () use ($app) {
return new PdoSessionStorage(
$app['pdo'],
$app['pdo.db_options'],
$app['session.storage.options']
);
});
Database structure
------------------
PdoSessionStorage needs a database table with 3 columns:
* ``session_id``: ID column (VARCHAR(255) or larger)
* ``session_value``: Value column (TEXT or CLOB)
* ``session_time``: Time column (INTEGER)
Examples of SQL statements to create the session table on `Symfony2 cookbook
<http://symfony.com/doc/current/cookbook/configuration/pdo_session_storage.html>`
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment