[英]Symfony 2 - FOSRestBundle - Call api method from another controller
I'm trying to call a method of my API but I get this exception : 我试图调用我的API的方法,但我得到这个例外:
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. 我尝试调用我的API的方法来创建用户,以便不重复相同的代码两次。 The API would be used by mobile device. API将由移动设备使用。 On a browser, when a user try to register, it calls a route that will call the API create user. 在浏览器上,当用户尝试注册时,它会调用将调用API创建用户的路由。 It's what I'm trying to do... 这就是我想要做的......
If I call directly the URL api for create user, it works perfectly. 如果我直接调用create api的URL api,它可以很好地工作。 app_dev.php/api/user/create
But when I call this method in my controller like here : 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'));
}
He is not able to find my userCreate method in UserController ... 他无法在UserController中找到我的userCreate方法...
Here is my UserController : 这是我的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 ? 我们只是试图调用API方法并返回结果...调用FOSRestBundle方法的最佳方法是什么?
Thanks 谢谢
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: 你需要 - 至少 - 为$this->get('...')
调用注入容器:
$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: 因为你似乎对symfony很新 - 这里有一些其他的提示:
Just use the following: 只需使用以下内容:
use Symfony\Component\HttpFoundation\JsonResponse;
return new JsonResponse($data, JsonResponse::HTTP_OK); // 200
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.