9951 explained code solutions for 126 technologies


php-symfonyHow to implement authentication with PHP Symfony?


Authentication with PHP Symfony can be implemented using the Security component. This component provides an authentication system that supports a variety of authentication mechanisms such as HTTP basic authentication, form login, and more.

Example code

use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
use Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider;
use Symfony\Component\Security\Core\User\InMemoryUserProvider;

$userProvider = new InMemoryUserProvider(array(
    'admin' => array('password' => 'adminpass', 'roles' => array('ROLE_ADMIN')),
    'user' => array('password' => 'userpass', 'roles' => array('ROLE_USER')),
));

$authProvider = new DaoAuthenticationProvider($userProvider);

$authManager = new AuthenticationProviderManager(array($authProvider));

Code explanation

  • InMemoryUserProvider: This class provides an in-memory user provider. It stores users in an array and is used for testing purposes.
  • DaoAuthenticationProvider: This class provides an authentication provider that uses a user provider to retrieve users and a user checker to check credentials.
  • AuthenticationProviderManager: This class manages an array of authentication providers.

Helpful links

Edit this code on GitHub