jstuardo
jstuardo

Reputation: 4383

Cannot call a command from a controller in Symfony2

I am trying to run a command from a controller but it does not work. This is my code:

            $email = $request->get('email');
            if (empty($email))
                $email = $request->get('nombres');
            if (empty($password))
                $password = '123456';                
            $application = new Application($this->container->get('kernel'));
            $application->setAutoExit(false);                 
            $input = new ArrayInput(array(
                "command" => "fos:user:create",
                "username" => $username,
                "email" => $email,
                "password" => $password));

            $output = new ConsoleOutput();
            $retval = $application->run($input, $output);

            var_dump(stream_get_contents($output->getStream()));
            die();

Simply it does nothing, #retval is 1 and the $output var is empty.

Any help will be appreciated Thanks

Jaime

Upvotes: 0

Views: 75

Answers (1)

l3l0
l3l0

Reputation: 3393

Basically you should not use command in controllers. Console command and Controller are two different delivery layers.

Please use services (so you can use it in controllers and in commands) for fos:user:create you can use something like that:

 $manipulator = $this->container->get('fos_user.util.user_manipulator');
 $manipulator->create($username, $password, $email, $active = true, $superadmin = false);

Upvotes: 1

Related Questions