The Blog

Frameworks

Symfony2 Doctrin2 encode password using prePersist Event Listener

Posted on

Service needs to look like this

services:
    user.listener:
        class: AppBundle\EventListener\UserListener
        arguments: [ "@security.password_encoder" ]
        tags: [ { name: doctrine.event_listener, event: prePersist } ]

Listener should look like that

// AppBundle\EventListener\UserListener.php

namespace AppBundle\EventListener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use AppBundle\Entity\User;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class UserListener
{
    protected $container;

    public function __construct(UserPasswordEncoderInterface $encoder)
    {
        $this->encoder = $encoder;
    }

    public function prePersist(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        $encodedPassword = $this->encoder->encodePassword($entity, $entity->getPassword());
        $entity->setPassword($encodedPassword);
    }
}
Posted in Symfony3 Leave a comment

Loading assets from a different server

Posted on

In a production environment you’ll eventually want to load assets (images, CSS and JS) from a different server. Perhaps you’re running your own asset server or you’re even using a CDN. This will require the files to be linked with an absolute URL. But in your development environment you don’t want this – you want assets to be loaded directly from the same server.
Read More

Posted in Symfony3 Leave a comment

Executing SQL directly in Symfony2 Doctrine

Posted on

Ok, so I’m moving project from non framework PHP to Symfony2, and it goes very nice, except of Doctrine, I’m sure it’s me. I’ve been using MySQL for last 8 years and DQL vs SQL to me feels like i.e. taking snowboard for the first time, after skiing for 30 years. What I do after 30 min. of trying. I’m going back to rental place and I switch to skis. Below one can find skis.
Read More

Posted in Frameworks, PHP, Symfony3 29 Comments
« Previous PageNext Page »