Reputation: 3
I'm running CakePHP 4.4.7 using PHP 8.0.23. I have a controller
class StrawberriesController extends AppController
{
public function eat($id = null)
{
\Cake\Log\Log::debug(json_encode($this->request->getMethod()));
$this->request->allowMethod(['get', 'post']);
...
}
}
I have a template with a form
<?= $this->Form->create($entity, [
'url' => [
'controller' => 'Strawberries',
'action' => 'eat',
$entity->id,
],
]) ?>
<?= $this->Form->text('is_asap') ?>
<?= $this->Form->submit('now') ?>
<?= $this->Form->end() ?>
When I submit the form, the debug line shows
2022-11-03 16:58:06 debug: "PUT"{
"scope": []
}
I expected to receive an HTTP POST request. Especially because using the devtools:
form
element has the attribute method="post"
So why is the HTTP method transformed by CakePHP from POST to PUT? How can I receive a POST request on my controller?
Upvotes: 0
Views: 55
Reputation: 130
When you pass an entity with an ID (an existing element) in the first parameter of your $this->Form->create()
CakePHP consider that is an update of your entity. And the convention for an update is to use "PUT" or "PATCH".
You can force the POST method like this :
$this->Form->create($entity, [
'url' => ['controller' => 'Strawberries', 'action' => 'eat', $entity->id],
'type' => 'POST'
]);
Doc : https://book.cakephp.org/4/en/views/helpers/form.html#options-for-form-creation
Upvotes: 2