1 回答
TA贡献1852条经验 获得超1个赞
根据 Slim 4 文档,您仍然可以使用请求正文中的参数或使用标头来覆盖表单方法。_METHOD
POST
X-Http-Method-Override
重要的一点(来自文档)是你需要添加到你的应用才能覆盖表单方法。Slim\Middleware\MethodOverrideMiddleware
下面是一个完全有效的示例:
<?php
require __DIR__ . '/../vendor/autoload.php';
use Slim\Factory\AppFactory;
$app = AppFactory::create();
$app->addRoutingMiddleware();
$app->add(new Slim\Middleware\MethodOverrideMiddleware);
$app->get('/', function($request, $response){
$form =<<<form
<form action="/put" method="post">
<input type="hidden" name="_METHOD" value="PUT"/>
<button type="submit">Send PUT request</button>
</form>
<form action="/delete" method="post">
<input type="hidden" name="_METHOD" value="DELETE"/>
<button type="submit">Send DELETE request</button>
</form>
form;
$response->getBody()->write($form);
return $response;
});
$app->put('/put', function($request, $response){
$response->getBody()->write('The request method is: ' . $request->getMethod());
return $response;
});
$app->delete('/delete', function($request, $response){
$response->getBody()->write('The request method is: ' . $request->getMethod());
return $response;
});
$app->run();
- 1 回答
- 0 关注
- 102 浏览
添加回答
举报