@@ -382,6 +382,10 @@ The Symfony2 HttpFoundation Component has a very powerful and flexible session
382
382
subsystem which is designed to provide session management through a simple
383
383
object-oriented interface using a variety of session storage drivers.
384
384
385
+ .. versionadded :: 2.1
386
+ The :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ SessionInterface ` interface,
387
+ as well as a number of other changes, are new as of Symfony 2.1.
388
+
385
389
Sessions are used via the simple :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session `
386
390
implementation of :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ SessionInterface ` interface.
387
391
@@ -404,7 +408,7 @@ Quick example::
404
408
Session API
405
409
~~~~~~~~~~~
406
410
407
- The :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session ` implements
411
+ The :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session ` class implements
408
412
:class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ SessionInterface.
409
413
410
414
The :class:`Symfony\\ Component\\ HttpFoundation\\ Session\\ Session ` has a simple API
@@ -413,18 +417,18 @@ as follows divided into a couple of groups.
413
417
Session workflow
414
418
415
419
* :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::start `:
416
- Starts the session - do not use `session_start() `.
420
+ Starts the session - do not use `` session_start() ` `.
417
421
418
422
* :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::migrate `:
419
- Starts the session - do not use `session_start() `.
423
+ Regenerates the session id - do not use `` session_regenerate_id() ` `.
420
424
421
425
* :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::invalidate `:
422
- Starts the session - do not use `session_start() `.
426
+ Clears the session data and regenerates the session id do not use `` session_destroy() ` `.
423
427
424
428
* :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::getId `: Gets the
425
429
session ID.
426
430
427
- * :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::setId `: Gets the
431
+ * :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::setId `: Sets the
428
432
session ID.
429
433
430
434
* :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::getName `: Gets the
@@ -457,9 +461,10 @@ Session attributes
457
461
Deletes an attribute by key;
458
462
459
463
* :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::clear `:
460
- Clear the bag ;
464
+ Clear all attributes ;
461
465
462
- Bag management
466
+ The attributes are stored internally in an "Bag", a PHP object that acts like
467
+ an array. A few methods exist for "Bag" management:
463
468
464
469
* :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session::registerBag `:
465
470
Registers a `Symfony\\Component\\HttpFoundation\\Session\\SessionBagInterface `
@@ -472,9 +477,6 @@ Bag management
472
477
Gets the `Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface `.
473
478
This is just a shortcut for convenience.
474
479
475
-
476
-
477
-
478
480
Save Handlers
479
481
~~~~~~~~~~~~~
480
482
@@ -483,7 +485,7 @@ session follows `open`, `read`, `write` and `close`, with the possibility of
483
485
`destroy ` and `gc ` (garbage collection which will expire any old sessions: `gc `
484
486
is called randomly according to PHP's configuration and if called, it is invoked
485
487
after the `open ` operation). You can read more about this at
486
- http:// php.net/session.customhandler
488
+ ` php.net/session.customhandle `_
487
489
488
490
489
491
Native PHP Save Handlers
@@ -519,7 +521,7 @@ Custom handlers are those which completely replace PHP's built in session save
519
521
handlers by providing six callback functions which PHP calls internally at
520
522
various points in the session workflow.
521
523
522
- Symfony2 HttpFoudation provides some by default and these can easily serve as
524
+ Symfony2 HttpFoundation provides some by default and these can easily serve as
523
525
examples if you wish to write your own.
524
526
525
527
* :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Storage\\ Handler\\ PdoSessionHandler `;
@@ -541,8 +543,8 @@ Session Bags
541
543
542
544
PHP's session management requires the use of the `$_SESSION ` super-global,
543
545
however, this interferes somewhat with code testability and encapsulation in a
544
- OOP paradigm. To help overcome this Symfony2 uses 'session bags' linked to the
545
- session to encapsulate a specific dataset like 'attributes' or 'flash messages'.
546
+ OOP paradigm. To help overcome this, Symfony2 uses 'session bags' linked to the
547
+ session to encapsulate a specific dataset of 'attributes' or 'flash messages'.
546
548
547
549
This approach also mitigates namespace pollution within the `$_SESSION `
548
550
super-global because each bag stores all its data under a unique namespace.
@@ -577,15 +579,16 @@ and remember me login settings or other user based state information.
577
579
578
580
* :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ AttributeBag `
579
581
This is the standard default implementation.
582
+
580
583
* :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Attribute\\ NamespacedAttributeBag `
581
584
This implementation allows for attributes to be stored in a structured namespace.
582
585
583
586
Any plain `key => value ` storage system is limited in the extent to which
584
- complex data can be stored since each key must be unique. You can achieve
587
+ complex data can be stored since each key must be unique. You can achieve
585
588
namespacing by introducing a naming convention to the keys so different parts of
586
- your application could operate without clashing. For example, `module1.foo ` and
589
+ your application could operate without clashing. For example, `module1.foo ` and
587
590
`module2.foo `. However, sometimes this is not very practical when the attributes
588
- data is an array, for example a set of tokens. In this case, managing the array
591
+ data is an array, for example a set of tokens. In this case, managing the array
589
592
becomes a burden because you have to retrieve the array then process it and
590
593
store it again.
591
594
@@ -639,19 +642,20 @@ Flash messages
639
642
The purpose of the :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Flash\\ FlashBagInterface `
640
643
is to provide a way of settings and retrieving messages on a per session basis.
641
644
The usual workflow for flash messages would be set in an request, and displayed
642
- on page redirect. For example, a user submits a form which hits an update
645
+ after a page redirect. For example, a user submits a form which hits an update
643
646
controller, and after processing the controller redirects the page to either the
644
- updated page or a error page. Flash messages set in the previous page request
647
+ updated page or an error page. Flash messages set in the previous page request
645
648
would be displayed immediately on the subsequent page load for that session.
646
649
This is however just one application for flash messages.
647
650
648
651
* :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Flash\\ AutoExpireFlashBag `
649
652
This implementation messages set in one page-load will
650
653
be available for display only on the next page load. These messages will auto
651
654
expire regardless of if they are retrieved or not.
655
+
652
656
* :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Flash\\ FlashBag `
653
657
In this implementation, messages will remain in the session until
654
- they are explicitly retrieved or cleared. This makes it possible to use ESI
658
+ they are explicitly retrieved or cleared. This makes it possible to use ESI
655
659
caching.
656
660
657
661
:class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Flash\\ FlashBagInterface `
@@ -673,7 +677,7 @@ has a simple API
673
677
Gets a flash by type (read only);
674
678
675
679
* :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Flash\\ FlashBagInterface::peekAll `:
676
- Gets all flashes (readoly );
680
+ Gets all flashes (read only );
677
681
678
682
* :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Flash\\ FlashBagInterface::has `:
679
683
Returns true if the type exists;
@@ -687,26 +691,26 @@ has a simple API
687
691
Testability
688
692
-----------
689
693
690
- Symfony2 is designed from the ground up with code-testability in mind. In order
691
- to make your code which utilises session easily testable we provide two separate
694
+ Symfony2 is designed from the ground up with code-testability in mind. In order
695
+ to make your code which utilizes session easily testable we provide two separate
692
696
mock storage mechanisms for both unit testing and functional testing.
693
697
694
698
Testing code using real sessions is tricky because PHP's workflow state is global
695
699
and it is not possible to have multiple concurrent sessions in the same PHP
696
700
process.
697
701
698
702
The mock storage engines simulate the PHP session workflow without actually
699
- starting one allowing you to test your code without complications. You may also
703
+ starting one allowing you to test your code without complications. You may also
700
704
run multiple instances in the same PHP process.
701
705
702
706
The mock storage drivers do not read or write the system globals
703
- `session_id() ` or `session_name() `. Methods are provided to simulate this if
707
+ `session_id() ` or `session_name() `. Methods are provided to simulate this if
704
708
required:
705
709
706
710
* :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ SessionStorageInterface::getId `: Gets the
707
711
session ID.
708
712
709
- * :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ SessionStorageInterface::setId `: Gets the
713
+ * :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ SessionStorageInterface::setId `: Sets the
710
714
session ID.
711
715
712
716
* :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ SessionStorageInterface::getName `: Gets the
@@ -753,14 +757,14 @@ handlers to PHP user-space.
753
757
In order to provide a solution for those using PHP 5.4, Symfony2 has a special
754
758
class called :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Storage\\ Handler\\ NativeSessionHandler `
755
759
which under PHP 5.4, extends from `\SessionHandler ` and under PHP 5.3 is just a
756
- empty base class. This provides some interesting opportunities to leverage
760
+ empty base class. This provides some interesting opportunities to leverage
757
761
PHP 5.4 functionality if it is available.
758
762
759
763
Save Handler Proxy
760
764
~~~~~~~~~~~~~~~~~~
761
765
762
- There are two kinds of save handler classes proxy which inherit from
763
- :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Storage\\ Handler\\ AbstractProxy `,
766
+ There are two kinds of save handler class proxies which inherit from
767
+ :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Storage\\ Handler\\ AbstractProxy `:
764
768
they are :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Storage\\ Handler\\ NativeProxy `
765
769
and :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Storage\\ Handler\\ SessionHandlerProxy `.
766
770
@@ -778,5 +782,8 @@ Under PHP 5.4 and above, all session handlers implement :phpclass:`SessionHandle
778
782
including `Native*SessionHandler ` classes which inherit from :phpclass: `SessionHandler `.
779
783
780
784
The proxy mechanism allow you to get more deeply involved in session save handler
781
- classes. A proxy for example could be used to encrypt any session transaction
785
+ classes. A proxy for example could be used to encrypt any session transaction
782
786
without knowledge of the specific save handler.
787
+
788
+
789
+ .. _`php.net/session.customhandle` : http://php.net/session.customhandler
0 commit comments