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
05d440f9
Commit
05d440f9
authored
Apr 07, 2011
by
Igor Wiedler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[SessionExtension] docs on how to use the SessionExtension
parent
5b1a28cf
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
0 deletions
+62
-0
doc/extensions.rst
doc/extensions.rst
+1
-0
doc/extensions/index.rst
doc/extensions/index.rst
+1
-0
doc/extensions/session.rst
doc/extensions/session.rst
+60
-0
No files found.
doc/extensions.rst
View file @
05d440f9
...
...
@@ -33,6 +33,7 @@ There are a few extensions that you get out of the box.
All of these are within the ``Silex\Extension`` namespace.
* :doc:`MonologExtension <extensions/monolog>`
* :doc:`SessionExtension <extensions/session>`
* :doc:`TwigExtension <extensions/twig>`
* :doc:`UrlGeneratorExtension <extensions/url_generator>`
...
...
doc/extensions/index.rst
View file @
05d440f9
...
...
@@ -5,5 +5,6 @@ Silex
:maxdepth: 2
monolog
session
twig
url_generator
doc/extensions/session.rst
0 → 100644
View file @
05d440f9
SessionExtension
================
The *SessionExtension* provides a service for storing data persistently
between requests.
Parameters
----------
* **session.storage.options**: An array of options that is passed to the
constructor of the ``session.storage`` service.
Services
--------
* **session**: An instance of Symfony2's `Session
<http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/Session.html>`_.
* **session.storage**: A service that is used for persistence of the
session data.
Registering
-----------
::
use Silex\Extension\SessionExtension;
$app->register(new SessionExtension());
Usage
-----
The Session extension provides a ``session`` service. Here is an
example that authenticates a user and creates a session for him::
use Symfony\Component\HttpFoundation\Response;
$app->get('/login', function() use ($app) {
$username = $app['request']->server->get('PHP_AUTH_USER', false);
$password = $app['request']->server->get('PHP_AUTH_PW');
if ('igor' === $username && 'password' === $password) {
$app['session']->set('user', array('username' => $username));
return $app->redirect('/account');
}
$response = new Response();
$response->headers->set('WWW-Authenticate', sprintf('Basic realm="%s"', 'site_login'));
$response->setStatusCode(401, 'Please sign in.');
return $response;
});
$app->get('/account', function() use ($app) {
if (null === $user = $app['session']->get('user')) {
return $app->redirect('/login');
}
return "Welcome {$user['username']}!";
});
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