Project Organization and Setup

Organizing an application using a micro-framework like Bullet can sometimes be a challenge because there is no pre-determined or enforced structure like there is with larger frameworks. This gives you a lot more freedom and control, but it also comes with questions about where to put certain things like configuration, templates, models, etc. and the best way to organize larger projects to maintain a good separation of concerns.

This page is offered as the recommended way to setup and organize an application built on Bullet so everyone on your team stays sane - even if that just means you.

Folder Organization

Typically, it is desireable to setup your project with a few different top-level directories that contain your different main content types, and a single web-accessible folder that contains the main index.php and all the frontend files.

The ideal project setup is provided in the bullet example skeleton app and looks like this:

  • app - All route files and anything related to this application
    • lang - Language files, if using I18N
    • routes - Route files
    • src - For custom PHP classes. Anything in here is PSR-0 Autoloaded.
    • templates - Template files
  • test - Tests for this application (any type)
  • vendor - Composer-created folder with Bullet and dependencies
  • web - All web-accessible files
    • assets - All your frontend asset files like images, CSS, and javascript
      • images - Image files
      • scripts - JavaScript files
      • styles - CSS/Stylesheets

Route Organization

The easiest way to organize your project is to separate all the router handlers for different base routes like posts, events, and comments into different same-named files, like posts.php, events.php, and comments.php. This is very similar to the way you would separate and organize your code in a larger MVC framework with controller classes. If you are following the folder structure above, these would be placed in app/routes. You would then require these files in your main web/index.php file that serves your Bullet application.

Your main index file would then look something like this:

define('BULLET_ROOT', dirname(__DIR__));
define('BULLET_APP_ROOT', BULLET_ROOT . '/app/');
define('BULLET_SRC_ROOT', BULLET_APP_ROOT . '/src/');
// Composer Autoloader
$loader = require BULLET_ROOT . '/vendor/autoload.php';
// Bullet App
$app = new Bullet\App(require BULLET_APP_ROOT . 'config.php');
$request = new Bullet\Request();
// Common include
require BULLET_APP_ROOT . '/common.php';
// Require all paths/routes
$routesDir = BULLET_APP_ROOT . '/routes/';
require $routesDir . 'index.php';
require $routesDir . 'events.php';
require $routesDir . 'messages.php';
require $routesDir . 'users.php';
// Response
echo $app->run($request);

So just like larger frameworks that have full MVC structures and controllers, a good separation of concerns can be maintained and all the routes and corresponding logic are broken up into more digestible independent files.

Fork me on GitHub