This is my controller
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request,AuthenticationUtils $authUtils)
{
// get the login error if there is one
$error = $authUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authUtils->getLastUsername();
return $this->render('blog/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
}
This is my security.yml
providers:
our_db_provider:
entity:
class: AppBundle:user
property: uname
in_memory:
memory:
users:
clement:
password: $2y$12$Z2B4JTnglzaYs4z73DBh9u/hIDN/E56CCrLKIjQxP6Q7aeLb5S6LO
roles: 'ROLE_ADMIN'
admin:
password: symfony
roles: 'ROLES_ADMIN'
ryan:
password: ryan1234
roles: 'ROLES_USER'
encoders:
Symfony\Component\Security\Core\User\User:
algorithm: bcrypt
cost: 12
AppBundle\Entity\User:
algorithm: bcrypt
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
# activate different ways to authenticate
# http://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
http_basic: ~
# http://symfony.com/doc/current/cookbook/security/form_login_setup.html
form_login:
login_path: login
check_path: login
secured_area:
pattern: ^/
provider: our_db_provider
anonymous: ~
logout: true
access_control:
# require ROLE_ADMIN for /admin*
- { path: ^/admin, roles: ROLE_ADMIN }
This is my view
<form action="{{ path('login') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
{#
If you want to control the URL the user
is redirected to on success (more details below)
<input type="hidden" name="_target_path" value="/account" />
#}
<button type="submit">login</button>
</form>
And I get this error.
Controller "AppBundle\Controller\SecurityController::loginAction()" requires that you provide a value for the "$authUtils" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.
This is not the correct answer to your question but a possible fix
public function loginAction(Request $request)
$authUtils = $this->get('security.authentication_utils');
at the beginning of your actionIf in Symfony 3.3 this feature is not working, you need to:
controller.service_arguments
This is however default setting in symfony, so you only need to change it if you cleared/replaced default services.yml.
If you don't want to use autowire, you can explicitly define argument types.