How to encode password in Silex?

584 Views Asked by At

I am writting a login app with Silex, but I have a problem with Silex password encoder. I read this in the silex document and got some code like this:

// find the encoder for a UserInterface instance
$encoder = $app['security.encoder_factory']->getEncoder($user);

// compute the encoded password for foo
$password = $encoder->encodePassword('foo', $user->getSalt());

But when I access my website in the first time, I don't have a $user varirable. Where can I get the $user varirable to encode my password?

UPDATE MY SOLUTION

Finally, I found a solution. This is my code to get encoded password:

$encoded = $app['security.default_encoder']->encodePassword($string, '')
1

There are 1 best solutions below

0
On BEST ANSWER

It's best to answer your question by giving yourself an answer. This gives you the ability to mark it as completed. Other users won't havae to look back to your question to give you a solution.

For this case, you can set my answer as completing so that the question is closed.

Use this code to encrypt your string:

$encoded = $app['security.default_encoder']->encodePassword($string, '')

You can also use other ways to encrypt your passwords in Silex. They can be found over here.

The following code could be used to select another encryption:

$app['security.default_encoder'] = function ($app) {
    return $app['security.encoder.pbkdf2'];
};