Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symfony requires that you provide a value for the $passwordEncoder argument #23033

Closed
shahzadthathal opened this issue Jun 2, 2017 · 7 comments

Comments

@shahzadthathal
Copy link

I'm following this article for simple registration form but getting this error when accessing the URL: http://localhost/symfony-app/web/app_dev.php/en/register

Controller "AppBundle\Controller\RegistrationController::registerAction()" requires that you provide a value for the "$passwordEncoder" 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. 500 Internal Server Error - RuntimeException

How can I fix this?

@chalasr
Copy link
Member

chalasr commented Jun 2, 2017

This looks like a documentation issue. This article has been updated in 3.3 and now assumes that your controller is tagged as controller.service_arguments, but nothing mentions that.

The standalone controller service configuration is missing, maybe assuming that you started your project using the new SE default configuration which makes use of the new automatic configuration related features. The problem is that you might very well not use all of them, I don't use this one myself and I think we should not assume they are used naturally all over the doc because we provided it as default.
Before, the sample controller was self-sufficient, extending the good class and not registered as service. The new one is better but incomplete. I think we should keep things explicit in the docs instead of relying on automatic discovery provided by default, because we can't be sure it is used (even if recommended, modularity is part of what makes symfony great to me, losing this would be too bad).

ping @weaverryan

@chalasr
Copy link
Member

chalasr commented Jun 2, 2017

For fixing your use case, register your controller as a service with the good tag, e.g:

AppBundle\Controller\RegistrationController:
    autowire: true # or define your controller constructor and configure its arguments explicitly
    tags: ['controller.service_arguments']

@xabbuh
Copy link
Member

xabbuh commented Jun 3, 2017

@chalasr Is right here. The documentation for 3.3 assumes that you have this config in your application. As this may not be the case when you just upgraded an existing application from 3.2 to 3.3 we may possibly improve the DX here a bit. I opened symfony/symfony-docs#7988 to keep track of it.

@xabbuh xabbuh closed this as completed Jun 3, 2017
@saraElskely
Copy link

public function newAction(Request $request)
{
$passwordEncoder = $this->get('security.password_encoder');
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $password = $passwordEncoder->encodePassword($user, $user->getPassword());
        $user->setPassword($password);
        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush();

        return $this->redirectToRoute('user_show', array('id' => $user->getId()));
    }

    return $this->render('TimeBundle:user:new.html.twig', array(
        'user' => $user,
        'form' => $form->createView(),
    ));
}

@pirvanm
Copy link

pirvanm commented Aug 28, 2017

I have a similar problem but a rest method from controller.
requires that you provide a value for the "$ref" 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.
Instantiating of object are made by me in a managerclass.

$obj = new Class();
$obj->setRef($ref);

@StellaRakoto
Copy link

Use Namespace :
"use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;" in the Controller

@Paularity
Copy link

Paularity commented Feb 7, 2019

try copying my code:

`<?php

namespace App\Controller;

use App\Entity\User;
use App\Form\RegistrationFormType;
use App\Security\LoginFormAuthenticator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;

class RegistrationController extends AbstractController
{
/**
* @route("/register", name="app_register")
*/
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder, GuardAuthenticatorHandler $guardHandler, LoginFormAuthenticator $authenticator): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // encode the plain password
        $user->setPassword(
            $passwordEncoder->encodePassword(
                $user,
                $form->get('plainPassword')->getData()
            )
        );

        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($user);
        $entityManager->flush();

        // do anything else you need here, like send an email

        return $guardHandler->authenticateUserAndHandleSuccess(
            $user,
            $request,
            $authenticator,
            'main' // firewall name in security.yaml
        );
    }

    return $this->render('registration/register.html.twig', [
        'registrationForm' => $form->createView(),
    ]);
}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants