Symfony FosRest 使用端点 api

Symfony FosRest use end point api

我需要在我的操作中调用一些方法(GET、POST 或 PUT),该怎么做。示例我有 PUT 端点

    /**
 * Update existing Team from the submitted data or create a new Team.
 *
 * @ApiDoc(
 * resource = true,
 * description = "Create/Update single Team",
 *  parameters={
 *      {"name"="name", "dataType"="string", "required"=false, "description"="image_back"}
 *  },
 * statusCodes = {
 *      200 = "Team successful update",
 *      400 = "Secret token is not valid"
 * },
 *  section="Team"
 * )
 * @RestView()
 *
 * @param Request $request
 * @param int     $id
 *
 * @return View
 */
public function putTeamAction(Request $request, $id)

并且此端点已溃败 "put_team"。如何在另一个控制器中调用此操作,例如我调用 POST entity Lead 并且在这个 post lead 中我需要调用 "put_team":

    /**
 * Post Lead.
 *
 * @ApiDoc(
 * resource = true,
 * description = "Post lead",
 *  parameters={
 *      {"name"="title", "dataType"="string", "required"=true, "description"="title lead"},
 * statusCodes = {
 *      200 = "Returned when successful",
 *      400 = "Returned secret token is not valid"
 * },
 * section="Lead"
 * )
 *
 * @RestView()
 *
 * @param Request $request
 *
 * @return View
 *
 * @throws NotFoundHttpException when not exist
 */
public function postLeadAction(Request $request)
{

如果我试试这个

    return $this->redirect($this->generateUrl('put_team', array('id' => 31)));

但是调用 get 方法,因为 url get 和 put 相等,并且默认情况下可能用于重定向 GET 方法

但我不需要 return,我需要调用一些 rout 并且仍然在第一个动作中工作,使用 rout "put_eam" 的答案 我试试

$test = $this->redirect($this->generateUrl('put_team', array('id' => 31)));

但有状态码 302 和

在内容 html 中:

<body>
    Redirecting to <a href="/app_dev.php/api/teams/31">/app_dev.php/api/teams/31</a>.
</body>

我用过这个,但是如何在帮助 Guzzle 的情况下这样使用呢?

function send_messages_aog($senders_token, $id, $title){

$url = BASE_URL.'/api/teams/'.$id;

$data = array('senders_token' => $senders_token, 'title' => $title);

$options = array(
    'http' => array(
        'method'  => 'PUT',
        'content' => json_encode( $data ),
        'header'=>  "Content-Type: application/json\r\n" .
            "Accept: application/json\r\n"
    )
);

$context  = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );

return $response;
}

已解决

我用 Guzzle

    public function updateTalent($userId, $token)
{
    $guzzle = new Client();
    $putTeam = $guzzle
        ->put($this->router->generate('put_developer', ['id' => $userId], UrlGeneratorInterface::ABSOLUTE_URL),
            [],
            json_encode([
                "token" => $token,
            ])
        )
        ->send()
        ->getBody(true);
    $answer = json_decode($putTeam, true);
    return $answer;
}

您可以使用 PHP cURL or install a PHP HTTP Client like Guzzle 进行 HTTP 请求。