Repeat after me, “We’re really great.” And our security system is almost
as cool as we are. So let’s keep up the pace and load users from the database
instead of the little list in security.yml.

What we’re about to do is similar to what the awesome open source FOSUserBundle
gives you. We’re going to build this all ourselves so that we really understand
how things work. Later, if you do use FOSUserBundle, you’ll be a lot
more dangerous with it.

Right now, this is just a plain, regular Doctrine entity that has nothing
to do with security. But, our goal is to load users from this table on login.
The first step is to make your class implement a UserInterface:

Second, add eraseCredentials. Keep this method blank for now. We will
add some logic to this later:

publicfunctioneraseCredentials(){// blank for now}

Finally, add getSalt() and just make it return null:

publicfunctiongetSalt(){returnnull;}

I’ll talk more about this method in a second.

Now that the User class implements UserInterface, Symfony’s authentication
system will be able to use it. But before we hook that up, let’s add the
yoda_user table to the database by running the doctrine:schema:update
command:

The one catch is that bcrypt isn’t supported until PHP 5.5. So if you’re
using PHP 5.4 or lower, you’ll need to install an extra library via Composer.
No problem! Head to your terminal and use the composer require command
and pass it ircmaxell/password-compat:

php composer.phar require ircmaxell/password-compat

When it asks, use the ~1.0.3 version. By the way, this require command
is just a shortcut that updates our composer.jsonfor us and then runs
the Composer update:

Leave a comment!

2015-01-21Diego Aguiar

Nice article, for the moment I'll follow the AppBundle structure for simplicity

Thanks for your time!

2015-01-21weaverryan

Hey Diego!

Actually, this is a big topic and something that's been changing! Though you don't see it (yet) in the Symfony series, the recommendation now (unless you're building code to be shared between projects) is to just have one bundle. You're of course free to add more if you feel like one feature is *really* different than everything else and it *feels* better to have it in a different bundle, but I would push you towards less bundles. Bundles are basically just directories, and having less directories and depth will make your project easier to navigate. In case you haven't seen it, here's a bit about the *why* behind the AppBundle: http://knpuniversity.com/bl...

Cheers!

2015-01-20Diego Aguiar

Hey there!

I have a question about creating bundles. What kind of logic should we follow in order to determine if is necessary to create a new bundle or not ?