After installing the Silex
and read some articles on Symfony 2
, I’ve come to a nice conclusion.
By default, it is only possible to include in the Symfony 2
values of the type chave/valor
.
It’s like it’s meant to be used like this:
$app['session']->set('nome', 'Wallace');
$app['session']->set('idade', '24 anos');
But through a class called Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag
, present in folder vendor
(that is part of the dependencies of Silex
), we can use the Session
similar to a Namespace
of PHP.
See test I performed as an example:
include_once 'vendor/autoload.php';
use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
$app = new Silex\Application();
$app->register(new Silex\Provider\SessionServiceProvider());
$app->get('/hello/{name}', function ($name) use ($app) {
// defino o valor que será utilizado como namespace
$bag = new NamespacedAttributeBag('container');
// adicionamos a instância da Bag
$app['session']->registerBag($bag);
// definimos os valores do container
$app['session']->set('container/nome', 'Wallace');
$app['session']->set('container/idade', '24 anos');
$data = $app['session']->get('container');
var_dump($data);
//Resultado: array(2) { ["nome"]=> string(7) "Wallace" ["idade"]=> string(7) "24 anos" }
return '';
});
$app->run();
That is to say:
$app['session']->set('container/nome', 'wallace');
is the same as:
$_SESSION['container']['nome'] = 'Wallace';
To end the day, I hope this helps!
Answers
1- How do I include more values in this Sesssion? Ex. city=>São Paulo
$app['session']->set('container/cidade', 'São Paulo');
2- How do I change for example the age from 21 to 30?
$app['session']->set('container/idade', '21');
echo $app['session']->get('container/idade'); // 21
$app['session']->set('container/idade', '30'); // define um novo valor
3- How do I delete something? ex: name
$app['session']->remove('container/nome');
4- How do I test if the key is created? Ex: If I want to test if the key age this set.
var_dump($app['session']->has('container/idade'))
And finally, to access the array with all the data
var_dump($app['session']->get('container'));
The first thing I would do is debug. Ever tried to give a
print_r(get_class_methods($app['session']))
? Maybe there’s something thereappend
oradd
– Wallace Maxters
Dude, if the
Silex
work with "dot Notation" equal to most frameworks, so maybe you just have to do$app['session']->set('container.nome', 'teste');
– Wallace Maxters
Wallace Silex uses Symfony 2 libraries. See clarifying documentation: http://api.symfony.com/2.6/Symfony/Component/HttpFoundation/Session/Session.html#replace()
– Joao Nivaldo
Wallace if I make $app['Session']->set('container.name', 'test'); this will create a new session named container.Name. Now running this you spoke print_r(get_class_methods($app['Session'])) it retouch me all the methods that are in the documentation whose link I passed up there. Only that I could not use them, in the documentation there is no example.
– Joao Nivaldo
You made me install the
Silex
, man! I want to help you :)– Wallace Maxters
Thank you very much.
– Joao Nivaldo