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

Feature request: RESTful controller skeleton

It'd be nice to have a skeleton for a RESTful API, with the basic controller methods already created. yes they're simple to write, but can easily be automated and will save a few minutes per model. When you have 40 models it's helpful!

it'd be nice if it could send the output to another method - that way, it could be transformed to an xml output, or a json output and wrapped around another class if needed.

custom controller templates would be nice.

I was going to submit this as an issue - but it stated that questions should be asked here instead.

phalcon controller MyModel --restful-json|restful-xml --model="MyNamespace\Models\MyModel" --base-class="MyNamespace\Controllers"

untested code - but you'll get the idea!

namespace MyNamespace\Controllers;

class MyModelController extends baseclass_if_passed
{
    private $model = 'MyNamespace\Models\MyModel`

    private function handleOutput($data)
    {
        // on this project i'm working on, all output is wrapped around a simple JsonOutput class with status, message, body for private clients to handle - but here, let's just output the code directly
        return $this->response->setJsonContent($data);
    }

    // get all / by id
    public function index($id = null)
    {
        // find by id
        if (!is_null($id))
        {
            return $this->handleOutput($this->model::FindFirst$id());
        }

        // return all?
        return $this->handleOutput($this->model::Find());
    }

    // post create
    public function create()
    {
        $data = ....
        $model = new $this->model;
        $model->save($data);
    }

    // put update
    public function update()
    {
        $data = ....
        $model = new $this->model;
        $model->save($data);    
    }

    // delete delete
    public function delete()
    {

    }

    // get search
    public function search($data)
    {
        // left blank for custom implementation or params passed to find function?
        // as Mongo would need custom implementation perhaps best to leave blank as a general template?
        $conditions = ...
        $data = $this->model::find([...]);
        return $this->handleOutput($data);
    }
}

We'll try to implement in 4.0.x branch