Slim 4 PUT 和 DELETE 表单提交
Slim 4 PUT and DELETE form submissions
如何在 Slim 4 中进行 PUT 和 DELETE 表单提交?我有以下路线:
$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 中的做法。
但它不会进入 editSubmit 方法,而是进入 createSubmit 方法。
如何使用这些方法提交?
According to Slim 4 documentation,您仍然可以在 POST
请求 body 中使用 _METHOD
参数或使用 X-Http-Method-Override
[=24] 覆盖表单方法=].
重要的一点 (from docs) 是您需要将 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();
如何在 Slim 4 中进行 PUT 和 DELETE 表单提交?我有以下路线:
$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 中的做法。
但它不会进入 editSubmit 方法,而是进入 createSubmit 方法。
如何使用这些方法提交?
According to Slim 4 documentation,您仍然可以在 POST
请求 body 中使用 _METHOD
参数或使用 X-Http-Method-Override
[=24] 覆盖表单方法=].
重要的一点 (from docs) 是您需要将 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();