2
\$\begingroup\$

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->registerNamespaces(
 array(
 'Controllers' => $appDir . '/controllers'
 )
)->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->mount($collection);
}
// Handle request
$app->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->setHandler('Controllers\MailController', true);
$mail->setPrefix('/public/mail');
// Define routes
$mail->get('/test', 'testAction');
return $mail;

MailController.php

namespace Controllers;
class MailController extends \Phalcon\Mvc\Controller
{
 public function testAction()
 {
 echo('test');
 }
}
200_success
146k22 gold badges190 silver badges478 bronze badges
asked Jan 14, 2015 at 8:21
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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 <- The model accessing the users table
 Auth.php <- Authentication
 Controller.php <- A master controller where all controllers inherit from
 Model.php <- 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.

answered Jan 15, 2015 at 16:18
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.