我已经开始按照 lumen 文档制作一些路由,其中显示了基本的 GET 和 POST 路由。
因此我尝试进行一些测试以了解它们的工作原理。虽然 GET 方法似乎按预期工作,但 POST 路由器似乎遇到了一些问题。在我的测试路由器下方:
$router->post('/foo', function ($req) {
var_dump($req); die();
});
然后我尝试使用 postman 发出 POST 请求,如下所示:
url : http://localhost:8000/foo
raw body of my request: {"key":"thisbodyrequestisdone"}
因此,我希望看到通过客户端在 http://localhost:8000/foo 发送我的主体 $req 参数的 var_dump .但它显示消息:
可能我遗漏了什么。有人能确切地告诉我如何在 lumen 中以正确的方式发出 POST 请求吗?感谢您的建议。
更新:下面是尝试使用 $req->all()
时的一些附加屏幕截图:
最佳答案
为了使其工作,您应该对 $req
变量进行类型提示,如文档所述:
To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request class on your controller constructor or method. The current request instance will automatically be injected by the service container Source
所以你的代码应该是:
$router->post('/foo', function (Request $req) {
var_dump($req); die();
});
还要确保使用 use Illuminate\Http\Request;
关于php - POST 请求不允许流明框架中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52471334/