Skip to content
Advertisement

How can I use UserPasswordEncoderInterface without declaring it in function parameter

How can I use UserPasswordEncoderInterface without having to declare it as a function parameter? I would like to use this method somewhere else and because of that I can’t.

namespace AppController;
/.../
use SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface;

class UserController extends AbstractController
{
     public function register(UserPasswordEncoderInterface $passwordEncoder, Request $request)
     {
     //some code here
     }

Advertisement

Answer

If you use autowire: true in your config you can inject the service. docs

namespace AppController;

use SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface;

class UserController extends AbstractController
{
    private UserPasswordEncoderInterface $passwordEncoder;

    public function __construct(UserPasswordEncoderInterface $passwordEncoder)
    {
        $this->passwordEncoder = $passwordEncoder;
    }

    public function register(Request $request)
    {
        // use it
        $this->passwordEncoder->encode();
    }
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement