You can read the intro here.
As I mentioned in the intro, the backend of my little project will be written in PHP, using FatFree. I like this framework because it is very light, very fast, and development with it feels natural to me. Instead of getting in the way and forcing me into its own system like Symfony, FatFree just gives me a nice and useful set of tools (routing, templates, etc) and lets me do my job.
Using composer, it is extremely easy to set this up.
$ composer require bcosca/fatfree
I'm also going to use Ikkez's Cortex library because it gives me a nicer ORM than the one in vanilla FatFree. In particular, it includes relationships and schemas.
On top of that, I'll add graphql-php to the mix, because one of my goals is to learn how to use GraphQL instead of jsonAPI.
Once again, as easy as:
$ composer require ikkez/f3-cortex
$ composer require webonyx/graphql-php
For now, that's it with the third-party libraries. Now, onto my own code structure.
I'm going to set most of the code in one directory, called lib. Inside it I will create model and controller. This is just my way of organizing my code, not a requirement from the framework.
The other important directory I need is web. This one will be the only public one once published, and will contain just two files, api.php and .htaccess.
This gives me a structure like:
+ vendor
+ lib
| + controller
| + model
+ web
| - api.php
| - .htaccess
- config.ini
- composer.json
- composer.lock
My initial config.ini will be very simple:
[globals]
DEBUG=3
AUTOLOAD=../lib/
DB_NAME=../db.sql
DEBUG manages the verbosity of the error traces, and 3 is the biggest value. Useful when developing.
AUTOLOAD adds the directory to the paths FatFree looks into when looking for classes. Basically, its own autoloader.
DB_NAME is there because, while developing, I'm going to use a basic SQLite database. Cortex makes it a breeze to switch to MySQL or others, which I'll probably do later on, but I prefer to start with something basic. This line then is the file where the database will be kept.
Finally, I'll just write a basic api.php that just loads up the config and sets an initial '/' route.
<?php
// composer autoloader for required packages and dependencies
require_once('../vendor/autoload.php');
// get an instance of FatFree
$f3 = \Base::instance();
// Load the config
$f3->config('../config.ini');
// Load or create the SQLite database
$f3->set('DB', new \DB\SQL('sqlite:'.$f3->get('DB_NAME')));
// Home
$f3->route("GET @home: /", function() {
echo "We're good";
});
// And finally, we launch FatFree
$f3->run();
We're just missing one thing, the .htaccess that redirects all routes to be handled by api.php. It's a basic one. I'll probably add a few tweaks later on, but for now it will do its job.
# Enable rewrite engine and route requests to framework
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* api.php [L,QSA]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]