Chapters
29 Chapters
|
1:34:37
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: >=5.3.3
Subscribe to download the code!Compatible PHP versions: >=5.3.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
27.
Doctrine Event Listeners
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=5.3.3",
"symfony/symfony": "~2.4", // v2.4.2
"doctrine/orm": "~2.2,>=2.2.3", // v2.4.2
"doctrine/doctrine-bundle": "~1.2", // v1.2.0
"twig/extensions": "~1.0", // v1.0.1
"symfony/assetic-bundle": "~2.3", // v2.3.0
"symfony/swiftmailer-bundle": "~2.3", // v2.3.5
"symfony/monolog-bundle": "~2.4", // v2.5.0
"sensio/distribution-bundle": "~2.3", // v2.3.4
"sensio/framework-extra-bundle": "~3.0", // v3.0.0
"sensio/generator-bundle": "~2.3", // v2.3.4
"incenteev/composer-parameter-handler": "~2.0", // v2.1.0
"doctrine/doctrine-fixtures-bundle": "~2.2.0", // v2.2.0
"ircmaxell/password-compat": "~1.0.3", // 1.0.3
"phpunit/phpunit": "~4.1", // 4.1.0
"stof/doctrine-extensions-bundle": "~1.1.0" // v1.1.0
}
}
5 Comments
Hello again...
// src/Yoda/UserBundle/Doctrine/UserListener.php
// ...
use Yoda\UserBundle\Entity\User;
// ...
private function handleEvent(User $user)
{
$plainPassword = $user->getPlainPassword();
$encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
$password = $encoder->encodePassword($plainPassword(), $user->getSalt());
$user->setPassword($password);
}
Remove brackets after $plainPassword please.
also it will be great if you could be more consistent and include RegisterController changes into the lesson. It is simple enough but may be useful for some students. So it maybe here just to be sure all on the same wave after the lesson.
Hey again!
Thanks - I've just fixed the code block at https://github.com/knpunive... About the RegisterController (the removal of the code at the end) - I think it's a good idea and we'll include a lot more when we update these tutorials soon. We have new code block technology that we can use to also help make things really clear - example http://knpuniversity.com/sc... :)
Cheers!
Hey Taco!
You're right in your thinking that it would make sense and be clean, etc. BUT, there is a practical problem: we don't have access to the service (security.encoder_factory) that's needed to encode the password from inside setPlainPassword(). In fact, your entities never have access to services (this is by design - to keep your entities simple). That's why we do it in a listener. If we were doing something simpler (e.g. when you call `setEmail()`, maybe you want to call `setUsername()` to be this same value, for some reason - a silly example) then we would have done it this way.
Does that help?
# src/Yoda/UserBundle/Resources/config/services.yml
services:
doctrine.user_listener:
class: Yoda\UserBundle\Doctrine\UserListener
arugments: ["@security.encoder_factory"]
tags:
- { name: doctrine.event_listener, event: prePersist }
1. There is a small typo that results in an error:
arugments => arguments
2. Another mistake on this page is here:
// src/Yoda/UserBundle/DataFixtures/ORM/LoadUsers.php
// ...
public function load(ObjectManager $manager)
{
// ...
// $admin->setPassword($this->encodePassword($admin, 'waynepass'));
$user->setPlainPassword('waynepass');
}
For the admin ($user->setPlainPassword('waynepass');) should be changed to:
$admin->setPlainPassword('waynepass');
Awesome, thanks! I fixed these at sha: https://github.com/knpunive... and am deploying them now.
Thanks Mihail!
"Houston: no signs of life"
Start the conversation!