POST 流明框架中不允许请求异常

POST request not allowed exception in lumen framework

我已经开始按照 lumen 文档制作一些路由,其中​​显示了基本的 GET 和 POST 路由。

因此我尝试进行一些测试以了解它们的工作原理。虽然 GET 方法似乎按预期工作,但 POST 路由器似乎遇到了一些问题。下面是我的测试路由器:

$router->post('/foo', function ($req) {
    var_dump($req); die();
});

然后我尝试使用邮递员发出 POST 请求,如下所示:

url : http://localhost:8000/foo
raw body of my request: {"key":"thisbodyrequestisdone"}

因此,我希望看到通过客户端在 http://localhost:8000/foo 发送我的正文 $req 参数的 var_dump。但它显示消息:

MethodNotAllowedHttpException

可能我遗漏了什么。有人能告诉我如何在 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;

导入请求 class

MethodNotAllowHttpExeptions - 修复它: 在项目文件夹中创建 .htaccess 文件,例如“Freelance.local/.htaccess”,然后将此代码放在那里。

RewriteEngine On 
RewriteRule ^(.*)$ public/ [L]