CakePHP: How to create a REST URL for a single action

99 Views Asked by At

I'm trying to set up and API for a database I maintain through CakePHP. So far I only need access to a single action on a single controller. I have managed to set up a new URL for the POST requests but the original URL still accepts these POSTs too.

Is there a way to stop the POST requests from being accepted by CakePHP unless it is send to the new URL? For example /contact/add is routed to /api/contact and should only accept POST requests there.

1

There are 1 best solutions below

1
On

In your routes.php you can configure the routing of contact/add to api/add As for the POST data, in your controller method just include something like the following:

public function add() {
        if($this->request->is('post')) {
             $this->autoRender = false;
             //handle api method here
        }

        else {
            //handle other requests here
        }
    }