我正在尝试调用我的API的方法但是我得到了这个例外:
FatalErrorException: Error: Call to undefined method
Project\Bundle\ApiBundle\Controller\UserController::userCreate() in
Symfony/src/Project/Bundle/UserBundle/Controller/UserController.php line 56
我尝试调用API的方法来创建用户,以便不重复相同的代码两次。 API将由移动设备使用。在浏览器上,当用户尝试注册时,它会调用将调用API创建用户的路由。这就是我想要做的......
如果我直接调用create api的URL api,它可以很好地工作。
app_dev.php/api/user/create
但是,当我在控制器中调用此方法时,就像这里:
public function registerAction()
{
$apiUserController = new \Moodress\Bundle\ApiBundle\Controller\UserController;
$answer = $apiUserController->userCreate();
return $this->redirect($this->generateUrl('moodress_home'));
}
他无法在UserController中找到我的userCreate方法...
这是我的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;
}
我们只是尝试调用API方法并获取结果... 调用FOSRestBundle方法的最佳方法是什么?
由于
答案 0 :(得分:2)
<强>溶液强>
你最好这样使用forwarding:
$response = $this->forward('AcmeYourBundle:User:userCreate', array());
// modify the response
return $response;
<强>解释强>
您试图调用不存在的操作。
public function userCreateAction() { /* ... */ }
......应该像这样调用:
$answer = $apiUserController->userCreateAction();
...而不是
$answer = $apiUserController->userCreate();
不要手动创建控制器对象:
此外,您不能只创建一个新的控制器对象并立即使用它
$controller = new \Moodress\Bundle\ApiBundle\Controller\UserController();
你需要 - 至少 - 为$this->get('...')
工作注入容器注入
$controller->setContainer($this->container);
你应该严重重构你的代码......行动方式太胖了。
<强>提示:强>
因为你似乎对symfony很陌生 - 这里有一些其他提示:
只需使用以下内容:
use Symfony\Component\HttpFoundation\JsonResponse;
return new JsonResponse($data, JsonResponse::HTTP_OK); // 200