How to work with Séssions at Silex

Asked

Viewed 325 times

5

Good night,

I have a question about how to include, change and delete data in the Silex Session. Its API documentation is very simple and without explanation.

Is the following:

I need to create a session containing an array of values I did so to create it:

$app['session']->set('container',['nome'=>'fulano', 'idade'=>21]);

Now the doubts begin.

1- How do I include more values in this Sesssion? Ex. city=>São Paulo

2- How do I change for example the age from 21 to 30?

3- How do I delete something? ex: name

4- How do I test if the key is created? Ex: If I want to test if the key age this set.

Thank you

  • 1

    The first thing I would do is debug. Ever tried to give a print_r(get_class_methods($app['session'])) ? Maybe there’s something there append or add

  • 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 Silex uses Symfony 2 libraries. See clarifying documentation: http://api.symfony.com/2.6/Symfony/Component/HttpFoundation/Session/Session.html#replace()

  • 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.

  • You made me install the Silex, man! I want to help you :)

  • Thank you very much.

Show 1 more comment

1 answer

3


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'));
  • Thank you very much. Tomorrow I test and speak to you. But it seems that so resolves everything. For now thank you very much.

  • I completed the answer :)

  • Wallace gave it right. Thank you very much. If you do not mind could explain to me how you found this library? Thank you very much.

  • @Joaonivaldo, it was at this link: Symfony2 Sessions Introduction

  • Please mark the answer as solved, if the problem has been solved, ok?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.