I've got a problem with my Symfony project. I'm trying to put a form login authenticator and for that I need to create a repository of my user class. However, i've got that error :
The autoloader expected class "AppBundle\Repository\UserRepository" to be defined in file "C:\wamp64\www\Symfony\vendor\composer/../../src\AppBundle\Repository\UserRepository.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
Here's my UserRepository class :
<?php
namespace AppBundle\Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\EntityRepository;
use AppBundle\Entity\User;
class UserRepository extends EntityRepository
{
public function findAllOrderedByName()
{
return $this->getEntityManager()
->createQuery(
'SELECT u FROM AppBundle:User u ORDER BY u.username ASC'
)
->getResult();
}
}
?>
My User entity :
<?php
namespace AppBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* @ORM\Table(name="user")
*/
class User implements UserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", unique=true)
*/
private $apiKey;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
public function getId()
{
return $this->id;
}
public function getUsername()
{
return $this->username;
}
public function getPassword()
{
return $this->password;
}
public function getApiKey()
{
return $this->apiKey;
}
public function getRoles()
{
return array('ROLE_USER');
}
public function getSalt()
{
}
public function eraseCredentials()
{
}
public function setUsername($_username)
{
$this->username = $_username;
}
public function setPassword($_password)
{
$this->password = $_password;
}
public function setId($_id)
{
return $this->id = $_id;
}
public function setApiKey($_apiKey)
{
return $this->apiKey = $_apiKey;
}
}
?>
My security.yml :
security:
encoders:
AppBundle\Entity\User:
algorithm: bcrypt
providers:
our_db_provider:
entity:
class: AppBundle:User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
guard:
authenticators:
- AppBundle\Security\LoginFormAuthenticator
And finally my FormLoginAuthenticator :
<?php
namespace AppBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
use AppBundle\Repository\UserRepository;
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{
private $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function supports(Request $request)
{
return $request->attributes->get('_route') === 'app_login'
&& $request->isMethod('POST');
}
public function getCredentials(Request $request)
{
return [
'username' => $request->request->get('username'),
'password' => $request->request->get('password'),
];
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
return $this->userRepository->findOneBy(['username' => $credentials['username']]);
}
public function checkCredentials($credentials, UserInterface $user)
{
die($user);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
// todo
}
protected function getLoginUrl()
{
// TODO: Implement getLoginUrl() method.
}
}
?>
Thanks you by advance for helping me. Also, if you have a better solution to build my form authenticator, it will be very nice.