Into the PWA: setting up the backend
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]
Hola @delcano, upv0t3
Este es un servicio gratuito para nuevos usuarios de steemit, para apoyarlos y motivarlos a seguir generando contenido de valor para la comunidad.
<3 Este es un corazón, o un helado, tu eliges .
: )
N0. R4ND0M:
6947 3669 8606 3988
4507 6628 2882 7819
2709 7599 4927 4181
1560 3035 7563 7969
Congratulations @delcano! You have received a personal award!
1 Year on Steemit
Click on the badge to view your Board of Honor.
Do not miss the last post from @steemitboard!
Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes
Congratulations @delcano! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Vote for @Steemitboard as a witness to get one more award and increased upvotes!