Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Is it possible to use the PUT and DELETE request methods in phalcon?

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

You can using PUT or DELETE methods with Phalcon Micro framework. Just write it simply like

<?php
$app->put('/product/{id}', function($id) {
    # your operation
});

$app->delete('/product/{id}', function($id) {
    # your operation
})

You can check this link for more information about phalcon micro.

edited 28d ago

Thank you good man, but I wanted to know if the same applies but using mvc of phalcon 3. Actually I don't use micro

Try with

$router->addDelete(
    '/ciclos/borrar/{ciclo_id:\d+}', [
        'controller' => 'ciclos', 
        'action' => 'delete' 
    ]
);

Sorry gentleman, I still can not get it, apparently the classic mvc phalcon does not support these methods, only GET and POST.



3.0k
Accepted
answer

HTML Forms can only use two methods: POST and GET.

To test if our PUT/DELETE routes are working as designed, you could send a cURL request to them.

edited 26d ago

HTML Forms can only use two methods: POST and GET.

To test if our PUT/DELETE routes are working as designed, you could send a cURL request to them.

Thank you for corraborating me. DELETE and PUT only works with micro.