Symfony:在 RuntimeException 中的路由结果中使用 put 请求获取

Symfony: fetch with put request on route results in RuntimeException

我正在尝试使用以下代码通过单击按钮来调用服务方法:

树枝HTML:

<input type="button" value="Ready Up" onclick="ready('{{ path('ready', {'playerName' : name}) }}')">

Javascript:

function ready(path) {
    fetch(path, {method : 'put'})
        .then(function (response) {
            console.log(response);
        });
}

Symfony 控制器:

/**
 * @Route("/api/player/ready", name="ready")
 * @return Response
 */
public function toggleReady($playerName) {
    $this->gameCacheService->readyUp($playerName);

    return new Response('test');
}

单击按钮会导致以下异常:

 Uncaught PHP Exception RuntimeException: "Controller "App\Controller\PlayerController::toggleReady()" requires that you provide a value for the "$playerName" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one." 

但是,生成的 URL 在我看来确实为“$playerName”提供了一个值:

/api/player/ready?playerName=Testname

是什么导致了这种行为?

在浏览器中呈现的最终 HTML:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
<body>
    <input type="button" value="Ready Up" onclick="ready('/api/player/ready?playerName=Testname')">
    <script>
        function ready(path) {
            fetch(path, {method : 'put'})
                .then(function (response) {
                    console.log(response);
                });
        }
    </script>
</body>
</html>

您忘记在注释中包含路由参数:

/**
 * @Route("/api/player/ready/{playerName}", name="ready")
 * @return Response
 */
public function toggleReady($playerName) {
    $this->gameCacheService->readyUp($playerName);

    return new Response('test');
}