Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2:从一个控制器向另一个控制器发送post请求_Php_Ajax_Symfony_Controller_Http Post - Fatal编程技术网

Php Symfony2:从一个控制器向另一个控制器发送post请求

Php Symfony2:从一个控制器向另一个控制器发送post请求,php,ajax,symfony,controller,http-post,Php,Ajax,Symfony,Controller,Http Post,我正在开发一个API方法,通过给定的json将用户保存到数据库中 方法如下所示: /** * Create user. * * @Rest\View */ public function cpostAction() { $params = array(); $content = $this->get("request")->getContent(); if(!empty($content)){ $params = json_decode

我正在开发一个API方法,通过给定的json将用户保存到数据库中

方法如下所示:

/**
 * Create user.
 *
 * @Rest\View
 */
public function cpostAction()
{
    $params = array();
    $content = $this->get("request")->getContent();
    if(!empty($content)){
        $params = json_decode($content, true);
    }

    $user = new User();
    $user->setUsername($params['username']);
    $user->setFbUserid($params['id']);

    $em = $this->getDoctrine()->getManager();
    $em->persist($user);
    $em->flush();

    return new Response(
      $this->get('serializer')->serialize($user, 'json'),
      201,
      array('content-type' => 'application/json')
    );
}
  $.ajax({
    type: "POST",
    url: "/api/users",
    data: '{"id":"11111","username":"someone"}'
  })
  .done(function(data){
  })
  .fail(function(data){
  });
我想用几种方式来调用它:一种是通过ajax调用,另一种是在不同的控制器中调用。 我的ajax请求如下所示:

/**
 * Create user.
 *
 * @Rest\View
 */
public function cpostAction()
{
    $params = array();
    $content = $this->get("request")->getContent();
    if(!empty($content)){
        $params = json_decode($content, true);
    }

    $user = new User();
    $user->setUsername($params['username']);
    $user->setFbUserid($params['id']);

    $em = $this->getDoctrine()->getManager();
    $em->persist($user);
    $em->flush();

    return new Response(
      $this->get('serializer')->serialize($user, 'json'),
      201,
      array('content-type' => 'application/json')
    );
}
  $.ajax({
    type: "POST",
    url: "/api/users",
    data: '{"id":"11111","username":"someone"}'
  })
  .done(function(data){
  })
  .fail(function(data){
  });
现在,来回答我的问题:我想在控制器中也这样做! 例如:

public function checkFBUserAction()
{
    $user_data = '{"id":"2222","username":"fritz"}'
    // post data to /api/users (router: _api_post_users) via POST
    // get response json back

}

如何实现这一点???

如果您想将此功能用作一项服务,更好的方法是将其移动到Symfony服务中,并在您的控制器中和您希望进入项目的位置调用此功能。

在第二个操作中,您可以转发请求:

public function checkFBUserAction()
{
   $user_data = array("id"=>"2222","username"=>"fritz");
   $this->getRequest->headers->set('Content-Type'), 'application/json');
   $this->getRequest()->request->replace(array(json_encode($user_data)));
   $this->forward('YourBundle:ControllerName:cpost');
}