|
| 1 | +.. index:: |
| 2 | + single: HTTP |
| 3 | + single: HttpFoundation, Sessions |
| 4 | + |
| 5 | +Integrating with legacy sessions |
| 6 | +================================ |
| 7 | + |
| 8 | +Sometimes it may be necessary to integrate Symfony into a legacy application |
| 9 | +where you do not initially have the level of control you require. |
| 10 | + |
| 11 | +As stated elsewhere, Symfony Sessions are designed to replace the use of |
| 12 | +PHP's native `session_*()` functions and use of the `$_SESSION` |
| 13 | +superglobal. Additionally, it is mandatory for Symfony to start the session. |
| 14 | + |
| 15 | +However when there really are circumstances where this is not possible, it is possible |
| 16 | +to use a special storage bridge |
| 17 | +:class:`Symfony\\Component\\HttpFoundation\\Session\\Storage\\PhpSessionStorage` |
| 18 | +which is designed to allow Symfony to work with a session started outside of |
| 19 | +the Symfony Session framework. You are warned that things can interrupt this |
| 20 | +use case unless you are careful: for example legacy application erases `$_SESSION`. |
| 21 | + |
| 22 | +Typical use of this might look as follows:: |
| 23 | + |
| 24 | + <?php |
| 25 | + use Symfony\Component\HttpFoundation\Session\Session; |
| 26 | + use Symfony\Component\HttpFoundation\Session\Storage\PhpSessionStorage; |
| 27 | + |
| 28 | + // legacy application configures session |
| 29 | + ini_set('session.save_handler', 'files'); |
| 30 | + ini_set('session.save_path', '/tmp'); |
| 31 | + session_start(); |
| 32 | + |
| 33 | + // Get Symfony to interface with this existing session |
| 34 | + $session = new Session(new PhpSessionStorage()); |
| 35 | + |
| 36 | + // symfony will now interface with the existing PHP session |
| 37 | + $session->start(); |
| 38 | + |
| 39 | +This will allow you to start using the Symfony Session API and allow |
| 40 | +migration of your application to Symfony Sessions. |
0 commit comments