We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

HTTP Method Restrictions

Hi, everybody!

I have the following router:

$di->set('router', function () {
    return require ROOT . '/app/config/routes.php';
});
<?php
$router = new Phalcon\Mvc\Router();

$router->removeExtraSlashes(true);

$router->addGet('/contacts', 'Contacts::index');
$router->addGet('/contacts/show/{id}', 'Contacts::show');
$router->addPost('/contacts/store', 'Contacts::store');
$router->addPut('/contacts/update/{id}', 'Contacts::update');
$router->addDelete('/contacts/destroy/{id}', 'Contacts::destroy');

return $router;

Why HTTP Method Restrictions doesn't work? E.g. The table entry (id: 1) is being deleted after opening the page 'something/contacts/destroy/1' using my browser (GET Method). So, I have to use the following check:

if($this->request->isDelete()){
code...
}

Thanks for your help.



98.9k
Accepted
answer

Hello, you have to disable default routes:

$router = new Phalcon\Mvc\Router(false);

https://docs.phalcon.io/en/latest/reference/routing.html#default-behavior

Hello, you have to disable default routes:

$router = new Phalcon\Mvc\Router(false);

https://docs.phalcon.io/en/latest/reference/routing.html#default-behavior

It works perfectly. Thank you, Phalcon!