简体   繁体   中英

Symfony FosRest use end point api

I need call some method (GET, POST or PUT) in my action, how do this. Example I have PUT endpoint

    /**
 * 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)

and this endpoint have rout "put_team". How to call this action in another controller, example I call POST entity Lead and in this post lead I need call "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)
{

If I try this

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

but call get methods, bacause url get and put equal and maybe for redirect by default GET method

But I don't need return, I need call some rout and still work in first action, using answer by rout "put_eam" I try

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

but have status code 302 and

in content html with:

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

I use this but how use like this with help 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;
}

SOLVED

I use 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进行HTTP请求或安装PHP HTTP Client(例如Guzzle)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM