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

why my api do not work via http Put and Delete, but work fine via Get and Post

router setting

    $router = new \Phalcon\Mvc\Router\Annotations(false);
    $router->setDefaultModule('index');
    $router->setDefaultNamespace('Ishgo\Index\Controllers');

    $router->addModuleResource('index', 'Ishgo\Index\Controllers\User', '/api/users');
    return $router;

API

    <?php
    namespace Ishgo\Index\Controllers;

    /**
     * @RoutePrefix("/api/users")
     */
    class UserController extends CommonController
    {
        /**
         * @Get("/?{id:\d*}")
         */
        public function indexAction($id=0)
        {
            if (empty($id)) {
                echo 'all users';
            } else {
                echo 'user by id:' . $id;
            }
        }

        /**
         * @Post("/{id:\d+}")
         */
        public function saveAction($id)
        {
            echo 'update user by id:' . $id;
        }

        /**
         * @Put("/")
         */
        public function addAction()
        {
            echo 'add user';
        }

        /**
         * @Delete("/{id:\d+}")
         */
        public function deleteAction($id)
        {
            echo 'delete user by id:' . $id;
        }

        /**
         * @Post("/login")
         */
        public function loginAction()
        {
            echo 'user login';
        }

    }


51.2k

For PUT method you should use:

$data = [];
parse_str(file_get_contents('php://input'), $data);

or if you send json body, you can get it with Phalcon like this:

$data = $this->request->getJsonRawBody();

I use PUT and DELETE, but not with annotations:

$router->addPut( $prefix . '/users/{user_id}', 'User::update');


9.3k
edited Mar '14

hi, i think that is router parse error, i can visit other action using http GET or POST method, but the PUT and DELETE didn't , that is very strange...

and the document have such usage: https://docs.phalcon.io/en/latest/reference/routing.html#annotations-router , i do not know what went wrong



51.2k

But are you sending the correct header ? Can you try with CURL ?

        $data = array('city' => 'Paris');
        $ch = curl_init('YOUR_API_URL');

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

        $response = curl_exec($ch);
        var_dump($response);


9.3k

i use chrome app(call postman) to visit the api, and i check the header, the http method is right



9.3k

i have uploaded my code to github, if you are convenient, please help me to find out the problems...

my code



51.2k

Barbery, for me it is working. I found only one problem. In your index.php file, on line 20 you have:

'Ishgo\Index\Controllers' => APP_PATH . '/index/Controllers/',

The folder Controlllers does not exists in your index module. It shoud be:

'Ishgo\Index\Controllers' => APP_PATH . '/index/controllers/',



9.3k

hi Calin Rada, i note the image you upload, the url you debug /api/users, but the router i setting is "@Put("/")", do not forget the last slash ...

about addAction

in my computer, i debug /api/users is work fine too, but /api/users/ didn't, why? according my setting, it should be /api/users/ work fine, /api/users didn't...

about deleteAction

do you try the deleteAction? because in my computer, that always do not work...

and thank you very much, thanks for your help