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

Route annotation DELETE method not working

I tried build restful api using phalcon route annonation GET, PUT, POST annotation is working but DELETE method is not. It just said not found method.

    /**
     * @Post("/")
     */
    public function addAction()
    {
        $level = $this->request->getPost("level");
        $ignore_limit = $this->request->getPost("ignore_limit");

        $api = new ApiKeysModel();
        $api->setKey($this->generateApiKey());
        $api->setIgnoreLimit((isset($ignore_limit) ? $ignore_limit : 0));
        $api->setLevel((isset($level) ? $level : 1));

        if ($api->save()) {
            return $this->apiResponse->withItem($api, new KeyTransformer());
        } else {
            return $this->apiResponse->errorInternalError();
        }

    }

    /**
     * @Delete("/")
     */
    public function deleteAction()
    {
        $key = "1";
        $api = ApiKeysModel::findFirst("key = '{$key}'");

        if (!$api) {
            return $this->apiResponse->errorInternalError();
        }

        $api->delete();

        $this->apiResponse->withArray(["deleted" => true]);
    }


8.1k

You are using different methods and actions for a single router - this is unacceptable. Yoou can appoint any methods for one action (route), but action must handle each methods request. You can change routes in this code

for example

/**
     * @Post("/")
     */
    public function addAction()
    {...}
    /**
     * @Delete("/")
     */
    public function deleteAction()
    {...}

to

/**
     * @Post("/add")
     */
    public function addAction()
    {...}
    /**
     * @Delete("/delete")
     */
    public function deleteAction()
    {...}


7.9k

I dont think that would be problem

as I said before other method except DELETE is working

    /**
     * @Get("/")
     */
    public function indexAction()
    {
        $apiKey = $this->request->getHeader("HTTP_X_API_KEY");
        $api = ApiKeysModel::findFirst("key = '{$apiKey}'");
        return $this->apiResponse->withItem($api, new KeyTransformer());
    }

    /**
     * @Put("/")
     */
    public function editAction()
    {
        $level = $this->request->getPut("level");
        $ignore_limit = $this->request->getPut("ignore_limit");

        $apiKey = $this->request->getHeader("HTTP_X_API_KEY");
        $api = ApiKeysModel::findFirst("key = '{$apiKey}'");

        $api->setLevel(isset($level) ? $level : $api->getLevel());
        $api->setIgnoreLimit(isset($ignore_limit) ? $ignore_limit : $api->getIgnoreLimit());

        $api->save();

        return $this->apiResponse->withItem($api, new KeyTransformer());
    }

    /**
     * @Post("/")
     */
    public function addAction()
    {
        $level = $this->request->getPost("level");
        $ignore_limit = $this->request->getPost("ignore_limit");

        $api = new ApiKeysModel();
        $api->setKey($this->generateApiKey());
        $api->setIgnoreLimit((isset($ignore_limit) ? $ignore_limit : 0));
        $api->setLevel((isset($level) ? $level : 1));

        if ($api->save()) {
            return $this->apiResponse->withItem($api, new KeyTransformer());
        } else {
            return $this->apiResponse->errorInternalError();
        }

    }

those code above is working only this dont work

    /**
     * @Delete("/")
     */
    public function deleteAction()
    {
        $key = "1";
        $api = ApiKeysModel::findFirst("key = '{$key}'");

        if (!$api) {
            return $this->apiResponse->errorInternalError();
        }

        $api->delete();

        $this->apiResponse->withArray(["deleted" => true]);
    }


8.1k

Is this code work for you through curl?

Curl request :


$ curl -X "DELETE" httop://your_site_route
/**
     * @Delete("/")
     */
public function deleteAction()
    {
        echo "Delete action", PHP_EOL;
    }


7.9k

It dont work in my end, but POST GET PUT works perfectly



8.1k

I use lighttpd server and I has not similar obstacle. All works.

You has Apache, may be. This is setting issue of Apache, may be.



7.9k

Yeah it might be, btw may I know what phalcon version installed in your pc?



8.1k
edited Nov '14

Yeah it might be, btw may I know what phalcon version installed in your pc?

1.3.4



7.9k
Accepted
answer

It was issue from 1.3.2 update to 1.3.4 and it works like a charm

thank you