I'm making a typical application with models, views and controllers.
I try to use the PUT method in the forms to edit data. When I send the request through PUT I get a 404 error. If I use POST the data is updated.
The same happens when I use DELETE to erase records. When I send the request through DELETE it shows 404. I must use POST to delete records.
According to this answer apparently is not possible but if it were possible, could someone give me an example?
I use the normal version 3.4.
I have the following code:
routes
<?php
$router->add(
'/ciclos/crear', [
'controller' => 'ciclos',
'action' => 'create'
]
)->via(['GET', 'POST']);
$router->add(
'/ciclos/actualizar/{ciclo_id:\d+}', [
'controller' => 'ciclos',
'action' => 'update'
]
)->via(['PUT']);
$router->add(
'/ciclos/borrar/{ciclo_id:\d+}', [
'controller' => 'ciclos',
'action' => 'delete'
]
)->via(['DELETE']);
controller
<?php
namespace App\Controllers;
use ...
class CiclosController extends ControllerBase
{
public function indexAction()
{
...
}
public function createAction()
{
if ($this->request->isPost()) {
...
} else {
...
}
}
function updateAction($ciclo_id)
{
if ($this->request->isPut()) {
// die($ciclo_id);
if ($ciclo->update() == false) {
// show an error
} else {
// all is fine
}
} else {
// show the view
}
}
public function deleteAction($ciclo_id)
{
if ($this->request->isDelete()) {
// die($ciclo_id);
if ($ciclo->delete() == false) {
// show an error
} else {
// all is fine
}
} else {
// show the view
}
}
volt view
<div class="row">
<div class="col-sm-12 features">
{{ form('ciclos/actualizar/' ~ ciclo.ciclo_id, 'method': 'post', 'id': 'frm') }}
<input name="_method" type="hidden" value="PUT">
<div class="form-group row">
<div class="col-md-6">
<label>Periodo</label>
<input type="text" class="form-control" name="periodo" placeholder="2019-2020" value="{{ old.periodo is defined ? old.periodo : ciclo.periodo }}">
</div>
</div>
<a href="{{ url('ciclos') }}" class="btn btn-default btn-cancel">
<i class="fa fa-chevron-circle-left"></i>
Cancel
</a>
<button type="reset" class="btn btn-danger btn-reset">
<i class="fa fa-undo"></i>
Clear
</button>
<button type="submit" class="btn btn-primary btn-continue" id="btn_continue">
<i class="fa fa-chevron-circle-right"></i>
<span class="text-btn">Continue</span>
</button>
{{ end_form() }}
{{ form('ciclos/borrar/' ~ ciclo.ciclo_id, 'method': 'post', 'id': 'frm') }}
<input name="_method" type="hidden" value="DELETE">
<button type="submit" class="btn btn-danger btn-delete" id="borrar_datos">
<span class="btn-txtb">Delete record</span>
</button>
{{ end_form() }}
</div>
</div>
Thank you