Skip to content

Developers documentation

dzey edited this page Sep 23, 2012 · 3 revisions

Framework

Mustached Robot is developed with FuelPHP. You should be familiar with this framework to start working on Mustached Robot. The documentation is nice and easy to work with.

Coding standards

As we use FuelPHP, we follow their coding standards.

Code organization

All the PHP code specific to Mustached Robot is available in /fuel/app/. Javascript and css are available in /public.

Modules

We use FuelPHP's [Modules] (http://docs.fuelphp.com/general/modules.html). It means most of the code (excepted the code needed across the whole application) is grouped into modules. Each module follow the same architecture:

|- module_1
|       |_ classes
|            |_ controller
|                  |_ api.php
|                  |_ admin.php
|                  |_ public.php
|            |_ model
|            |_ single_class.php
|       |_ views
|       |_ config
|- module_2
|       |_ classes
|            |_ controller
|            |_ model
|            |_ other_class.php
|       |_ views
|       |_ config

Controllers

Each module has controllers. These controllers must inherit from one on the three main base controllers (these main controllers are available in /fuel/app/classes/controller):

  • \Front_Controller -- Used to display information for a member or a public user
  • \Admin_Controller -- Used for administrators
  • \API_Controller -- Used to return data

Model

We use FuelPHP's ORM. See their documentation for more information

Views

We use Sensio Lab's Twig as a template engine. To call a view from within a controller, just call return $this->_render('template_name'); (without the .twig extension). This will call the template called 'template_name.twig' located in the views directory of the module.

These views inherits from layouts defined in /fuel/app/views

Other classes

In the 'classes' folder of each module, we can also have classes other than Controller or Model. We usually have at least two classes:

  • Manager: handles the business and database logic
  • Form: handles the form creation and update

But you can also have other classes.

Tests

Each module also has its own tests. See the FuelPHP documentation on Unit Testing.

To configure the testing environment, you need to install PHPUnit and a database called mustached_test. To populate this database, run the following command at the root of the project:

	php oil refine db:setup_test

To run the tests, you just need to run the command (at the root of the project):

	php oil test

You can also run test for a specific group:

	php oil test --group=App

(here, only the Mustached Robot-specific code will be tested, without testing the framework).

The project is automatically tested at each push on Github with Travis.

Less

-- Important changes since @9b3dc12079 --

We use less to generate css files. Less files are located in /public/assets/less directory.

The developer must compile the less himself in the /public/assets/css directory (using less compiler or specific application such as Less.app.

When a request is made, the Framework generates a combined-minified-versioned version of the CSS files (thanks to the Casset package). These versions are stored in the /public/assets/cache/ directory (which must be writable)).

CSS files are loaded in the Framework in the Controller_Base (/fuel/app/classes/controller/base.php) and called in the app.twig layout with the "render_css()" method.

Javascript

Javascript files are stored in the /public/assets/js directory. As for the CSS, a combined-minified-versioned version of the JS files are created and served when a request is made. This version is stored in the /public/assets/cache directory.

As for the CSS, JS files are loaded in the Framework in the Controller_Base (/fuel/app/classes/controller/base.php) and called in the app.twig layout with the "render_js()" method.

Users

We use FuelPHP's SimpleAuth package to handle authentication.

In each controller, you have access to a variable called current_user. If this variable is set, it means a user is logged in. You also have access to some variables :

  • user_id
  • firstname
  • email
  • group : this is the group the user belongs to. See SimpleAuth documentation -- basically : group = 100 means it is an admin and group = 1 means it's a simple user

You can access this datas in the Controller :

  • $this->current_user['user_id']
  • $this->current_user['firstname']
  • $this->current_user['group']
  • $this->current_user['email']

Or in the templates :

Twig :

  • {{ current_user.user_id }}
  • {{ current_user.firstname }}
  • {{ current_user.group }}
  • {{ current_user.email }}

You can also access the user's avatar with the twig function {{ avatar(current_user.email, 40) }} (40 can be replaced by another integer: it is the size of the avatar).

For example, to test if the logged user is an admin in a template, you can do:

	{% if current_user.group == 100 %}I'm an admin!{% endif %}