Adding an ActiveRecordController to Alpha

A new core controller should be added to Alpha to handle CRUD requests.

Looking for a new simple way to plan your day? Get organized with Five.Today


Lately in my work, I have become utterly fed up writing Credit Read Update Delete (CRUD) APIs, writing similar code several times over, which of course completely violates the DRY (Don't Repeat Yourself) principal. I am convinced that 80% of the HTTP requests that go to a typical HTTP REST API are CRUD, therefore I have decided to add a generic CRUD controller to Alpha1.

The ActiveRecordController due for Alpha 2.0

The public API

(POST|GET|PUT|DELETE) /record/{ActiveRecordType}/{ActiveRecordOID}

So the API will need to receive the ActiveRecordType parameter to indicate the type of record to operate on, and optionally the ActiveRecordOID when we are working on a single record.

The code

The following code will be added to the FrontController:

$this->addRoute('/record/{ActiveRecordType}/{ActiveRecordOID}', function($request) {
    $controller = new ActiveRecordController();
    return $controller->process($request);
})->value('ActiveRecordOID', null);

The ActiveRecordOID parameter is made optional via the value() call because it is not required by POST requests. Naturally, the implementation of the Alpha\Controller\ActiveRecordController class will contain all of the methods required to marshal HTTP method calls to database CRUD calls using the new ActiveRecord interface coming in ALpha 2.02.


Once this class is in place, a number of views and controllers will need to be updated to use it. The effort to do that will be worth it, as it will reduce the overall lines of code in Alpha further, and centralize common logic in one place.

It would also be possible to make the ActiveRecordOID parameter optional for GET requests and make the controller return a list of records of type ActiveRecordType, however then the new controller would also have to support start/limit parameters for pagination. One for further investigation on the ticket3.


Looking for a modern PHP framework to build your new project? Please consider using the Alpha Framework

John Collins

I have been writing about web technology and software development since 2001. I am the developer of the Alpha Framework for PHP, and the Five.Today personal productivity app. I love open source, technology, and economics.