Recently I had to implement a REST API on a website developed on CakePHP.

CakePHP is a great framework and has its own (easy) ways to setup a REST API. But I needed something a bit more flexible and a bit more strict. I needed more than CRUD functionality, I wanted everything to be done through POSTs and I wanted everything to be somewhat centralised in a special controller.
I also wanted to reuse the CRUD functions defined in each controller for the admin backend by forwarding the API calls to these functions.

It’s not perfect, I just implemented it, probably different issues will show up and I’ll have to modify it a bit, but here it is.

Prepare the routing:

The ApiController:

Add an api_key column to your users table. You can create the key however you want, just make sure it is unique.
Now you can POST to “/api/posts/list.json” using as username for your Basic Auth the API key.

That’s all!