超薄 4 放置和删除表单提交

如何在 Slim 4 中执行 PUT 和删除表单提交?我有以下路线:


$group->get('/sites/create', SitesController::class . ':create')->setName('sites_create');

$group->get('/sites/{id}/edit', SitesController::class . ':edit')->setName('sites_edit');

$group->post('/sites', SitesController::class . ':createSubmit')->setName('sites_create_submit');

$group->put('/sites', SitesController::class . ':editSubmit')->setName('sites_edit_submit');

这是我的:


<form action="/sites" method="POST">

    <label for="name">Site name</label>

    <input type="text" class="form-control" id="name" name="name">                 

    <input type="hidden" name="_METHOD" value="PUT">

    <button type="submit">Submit</button>

</form>

这就是我在Slim 3中会如何做到的。


但它不会转到编辑提交方法,而是转到创建提交方法。


如何使用这些方法提交?


当年话下
浏览 103回答 1
1回答

小怪兽爱吃肉

根据 Slim 4 文档,您仍然可以使用请求正文中的参数或使用标头来覆盖表单方法。_METHODPOSTX-Http-Method-Override重要的一点(来自文档)是你需要添加到你的应用才能覆盖表单方法。Slim\Middleware\MethodOverrideMiddleware下面是一个完全有效的示例:<?phprequire __DIR__ . '/../vendor/autoload.php';use Slim\Factory\AppFactory;$app = AppFactory::create();$app->addRoutingMiddleware();$app->add(new Slim\Middleware\MethodOverrideMiddleware);$app->get('/', function($request, $response){&nbsp; &nbsp; $form =<<<form&nbsp; &nbsp; <form action="/put" method="post">&nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" name="_METHOD" value="PUT"/>&nbsp; &nbsp; &nbsp; &nbsp; <button type="submit">Send PUT request</button>&nbsp; &nbsp; </form>&nbsp; &nbsp; <form action="/delete" method="post">&nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" name="_METHOD" value="DELETE"/>&nbsp; &nbsp; &nbsp; &nbsp; <button type="submit">Send DELETE request</button>&nbsp; &nbsp; </form>form;&nbsp; &nbsp; $response->getBody()->write($form);&nbsp; &nbsp; return $response;});$app->put('/put', function($request, $response){&nbsp; $response->getBody()->write('The request method is: ' . $request->getMethod());&nbsp; return $response;});$app->delete('/delete', function($request, $response){&nbsp; &nbsp; $response->getBody()->write('The request method is: ' . $request->getMethod());&nbsp; &nbsp; return $response;});$app->run();
打开App,查看更多内容
随时随地看视频慕课网APP