Question

I'm trying to call a method of my API but I get this exception :

FatalErrorException: Error: Call to undefined method 
Project\Bundle\ApiBundle\Controller\UserController::userCreate() in
Symfony/src/Project/Bundle/UserBundle/Controller/UserController.php line 56

I try to call the method of my API for create a user in order to not repeat the same code twice. The API would be used by mobile device. On a browser, when a user try to register, it calls a route that will call the API create user. It's what I'm trying to do...

If I call directly the URL api for create user, it works perfectly. app_dev.php/api/user/create But when I call this method in my controller like here :

public function registerAction()
{
  $apiUserController = new \Moodress\Bundle\ApiBundle\Controller\UserController;
  $answer = $apiUserController->userCreate();
  return $this->redirect($this->generateUrl('moodress_home'));
} 

He is not able to find my userCreate method in UserController ...

Here is my UserController :

class UserController extends Controller
{
    /**
    * @Post("/user/create")
    */
    public function userCreateAction()
    {
    $userManager  = $this->get('fos_user.user_manager');
    $jsonErrorCreator = $this->get('moodress_api.create_error_json');

    $user         = $userManager->createUser();
    $user->setEnabled(true);
    $request      = $this->get('request');
    $name         = $request->request->get('name');
    $email        = $request->request->get('email');
    $password     = $request->request->get('password');
    $verification = $request->request->get('verification');

    // We check if all parameters are set
    $code = 0;
    if ($name === null)                                        $code = 200;
    else if ($email    === null)                               $code = 201;
    else if ($password === null)                               $code = 202;
    else if ($verification === null)                           $code = 203;
    else if ($password !== $verification)                      $code = 204;
    else if ($userManager->findUserByUsername($name) !== null) $code = 205;
    else if ($userManager->findUserByEmail($email)   !== null) $code = 206;
     if ($code != 0)
                return ($jsonErrorCreator->createErrorJson($code, null));
    // We set parameters to user object
    $user->setUsername($name);
    $user->setEmail($email);
    $encoder = $this->get('security.encoder_factory')->getEncoder($user);
    $password_user = $encoder->encodePassword($password, $user->getSalt());
    $user->setPassword($password_user);

    $clientManager = $this->get('fos_oauth_server.client_manager.default');
    $client = $clientManager->createClient();
    $client->setRedirectUris(array('http://localhost:8888/app_dev.php'));
    $client->setAllowedGrantTypes(array('token', 'authorization_code'));
    $clientManager->updateClient($client);

    $user->setClient($client);
    // We save the user in the database
    $userManager->updateUser($user);

    // We use the json service for returning the data
    $jsonCreator = $this->get('moodress_api.create_json');
    $response = $jsonCreator->createJson($user);
    return $response;
}

We are just trying to call API method and get back the result... What is the best way for calling a method of the FOSRestBundle ?

Thanks

Was it helpful?

Solution

solution:

You should probably better use forwarding like this:

$response = $this->forward('AcmeYourBundle:User:userCreate', array());
// modify the response

return $response;

explanation:

You tried to call a non-existant action.

public function userCreateAction() { /* ... */ }

... which should be called like this:

$answer = $apiUserController->userCreateAction();

... instead of

$answer = $apiUserController->userCreate();

Don't create controller objects manually:

Further you can't just create a new controller object and use it instantly

$controller = new \Moodress\Bundle\ApiBundle\Controller\UserController();

you need to - at least - inject the container for $this->get('...') calls to work:

$controller->setContainer($this->container);

You should heavily refactor your code though ... the action is way too fat.

tips:

As you seem to be quite new to symfony - here are a few other tips:

  • you should create a login form-type and use this instead of obtaining data from the request directly ( or use the one already provided by FOSUserBundle )
  • you should add validation metadata to your user class and validate it using the validator service ( User validation-mappings are already provided by FOSUserBundle )
  • there is a JsonResponse class that you can use
  • HTTP status codes are constants in the request class that you can use

Just use the following:

use Symfony\Component\HttpFoundation\JsonResponse;

return new JsonResponse($data, JsonResponse::HTTP_OK); // 200
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top