PHP/Phalcon RESTful API architecture I'm building a RESTful API in PHP using the Phalcon framework. The API will consist of multiple modules (e.g. api.example.com/mail/users, api.example.com/web/users, etc). So I want to put all module specific logic at one place as much as possible.

I have a lot of programming experience but PHP is all pretty new to me. So if you could help me determine if I'm on the right track, so that this code won't give me future problems, or if I'm missing something or if the code could be abstracted even more, then that would be great.

Directory structure:




app/
    collections/
        MailCollection.php
    config/
        collections.php
    controllers/
        MailController.php
public/
    index.php



index.php



// Set path variables
$dir = dirname(__DIR__);
$appDir = $dir . '/app';


// Create autoLoader
$loader = new \Phalcon\Loader();

// Register namespaces
$loader-&gt;registerNamespaces(
    array(
        'Controllers' =&gt; $appDir . '/controllers'
    )
)-&gt;register();

// Create dependency injector
$di = new \Phalcon\DI\FactoryDefault();

// Create micro application
$app = new Phalcon\Mvc\Micro($di);

// Mount the collections
$collections = include_once($appDir . '/config/collections.php');
foreach($collections as $collection) {
    $app-&gt;mount($collection);
}

// Handle request
$app-&gt;handle();


collections.php

// Add collections
$collections[] = (
    include_once($dir . '/app/collections/MailCollection.php')
);

return $collections;


MailCollection.php

use \Phalcon\Mvc\Micro\Collection as MicroCollection;

// Setup Collection
$mail = new MicroCollection();
$mail-&gt;setHandler('Controllers\MailController', true);
$mail-&gt;setPrefix('/public/mail');

// Define routes
$mail-&gt;get('/test', 'testAction');

return $mail;


MailController.php

namespace Controllers;

class MailController extends \Phalcon\Mvc\Controller
{
    public function testAction()
    {
        echo('test');
    }
}
I believe you are on the right track.

The only thing that I would do different would be to add a library folder in which I would store all my common stuff like models, utility classes, helpers etc. I would also namespace everything so as to avoid collisions with any other application or piece of code out there. 

My preferred app structure for your app would be as follows

app/
    mail/
        controllers/
            MailController.php
    web/
        controllers/
            WebController.php
library/
    MyAPI/
        models/
            Users.php   &lt;- The model accessing the users table
        Auth.php        &lt;- Authentication
        Controller.php  &lt;- A master controller where all controllers inherit from
        Model.php       &lt;- A master model that all models inherit from
        Locale.php
        Utils.php
var/
    config/
        collections.php
        db.php
        config.php
    cache/
        dummy.txt
    metadata/
        dummy.txt
    logs/
        dummy.txt
public/
    index.php


Your controllers inherit from the base controller in the library folder. In that controller you can do common tasks for all collections. The same applies to the master model.

You can remove the controllers/ folder under each module in the app folder and have them all right under mail, web etc. It's up to you.