JSON REST API which is build on top of Symfony framework.
Note that this project is build with Symfony 4 and Symfony Flex.
- What is this?
- PHP 7.2 or higher
- Composer
- Database that is supported by Doctrine
- Sodium crypto library (libsodium)
Use your favorite IDE and get checkout from GitHub or just use following command
git clone https://github.com/tarlepp/symfony-flex-backend.gitNext you need to create .env file, which contains all the necessary
environment variables that application needs. You can create it by following
command (in folder where you cloned this project):
cp .env.dist .envThen open that file and make necessary changes to it. Note that this .env
file is ignored on VCS.
note that this optional way to do this, installing dependencies will prompt you on every needed configuration
Next phase is to install all needed dependencies. This you can do with following command, in your project folder:
composer installOr if you haven't installed composer globally
curl -sS https://getcomposer.org/installer | php
php composer.phar installApplication uses JWT to authenticate users, so we need to create public and private keys to sign those. You can create new keys with following command.
make generate-jwt-keysNext thing is to make sure that application var directory has correct
permissions. Instructions for that you can find
here.
I really recommend that you use ACL option in your development environment.
To check that your environment is ready for this application. You need to make two checks; one for CLI environment and another for your web-server environment.
You need to run following command to make all necessary checks.
./vendor/bin/requirements-checkerOpen terminal and go to project root directory and run following command to start standalone server.
./bin/console server:startOpen your favorite browser with http://127.0.0.1:8000/check.php url and
check it for any errors.
To get JWT authorization headers to work correctly you need to make sure that your Apache config has mod_rewrite enabled. This you can do with following command:
sudo a2enmod rewriteIf you want to allow another IP addresses or all to your dev environment
see /allowed_addresses.php file for detailed information how you can allow
certain IP addresses to have access to your dev environment.
Symfony Flex comes with Makefile configuration so that you can easily run
some generic commands via make command. Below is a list of currently
supported (main commands) make commands, note that you can get this same list
with just running make command:
cache-clear # Clears the cache
cache-warmup # Warms up an empty cache
check-vendor-dependencies # Checks if any vendor dependency can be updated
clear-vendor-bin # Runs PHPStan static analysis tool
ecs # Runs The Easiest Way to Use Any Coding Standard
generate-jwt-keys # Generates JWT auth keys
merge-clover # Creates clover from fastest run
merge-junit # Creates JUnit xml from fastest run
phpcs # Runs PHP CodeSniffer
phpmetrics # Generates PhpMetrics static analysis
phpstan # Runs PHPStan static analysis tool
psalm # Runs Psalm static analysis tool
run-tests-fastest-phpdbg # Runs all test via fastest (phpdbg)
run-tests-fastest-php # Runs all test via fastest (pure PHP)
run-tests-fastest # Runs all test via fastest (Uses phpdbg if that is installed)
run-tests-phpdbg # Runs all tests via phpunit (phpdbg)
run-tests-php # Runs all tests via phpunit (pure PHP)
run-tests # Runs all tests via phpunit (Uses phpdbg if that is installed)
serve # Runs a local web server
You can list all Symfony console commands via following command:
./bin/consoleProject contains following custom console commands to help eg. user management:
./bin/console user:management # To manage your users and user groups
./bin/console api-key:management # To manage your API keys
./bin/console make:rest-api # To create skeleton classes for new REST resource
./bin/console utils:create-date-dimension-entities # Console command to create 'DateDimension' entities.This command is just a wrapper for following commands:
./bin/console user:create # To create user
./bin/console user:create-group # To create user group
./bin/console user:create-roles # To initialize user group roles
./bin/console user:edit # To edit user
./bin/console user:edit-group # To edit user group
./bin/console user:remove # To remove user
./bin/console user:remove-group # To remove user group
./bin/console user:list # To list current users
./bin/console user:list-groups # To list current user groupsThis command is just a wrapper for following commands:
./bin/console api-key:create # To create API key
./bin/console api-key:edit # To edit API key
./bin/console api-key:change-token # To change API key token
./bin/console api-key:remove # To remove API key
./bin/console api-key:list # To list API keysCommand to create DateDimension entities that can be used with date/time related report queries.
todo
I highly recommend that you use "proper" IDE to development your application. Below is short list of some popular IDEs that you could use.
Personally I recommend PhpStorm, but just choose one which is the best for you.
Also note that project contains .idea folder that holds default settings for
PHPStorm.
It's highly recommended that you use this tool while doing actual development
to application. PHP Code Sniffer is added to project dev dependencies, so
all you need to do is just configure it to your favorite IDE. So the phpcs
command is available via following example command.
./vendor/bin/phpcs -iIf you're using PhpStorm following links will help you to get things rolling.
When you start a new project where you use this project as a "seed" first thing to do is to run following command:
./bin/console doctrine:migrations:diffThis will create a migration file which contains all necessary database changes to get application running with default database structure. You can migrate these changes to your database with following command:
./bin/console doctrine:migrations:migrateAfter that you can start to modify or delete existing entities or create your own ones. Easiest way to make this all work is to follow below workflow:
- Make your changes (create, delete, modify) to entities in
/src/Entity/folder - Run
diffcommand to create new migration file - Run
migratecommand to make actual changes to your database - Run
validatecommand to validate your mappings and actual database structure
Those commands you can run with ./bin/console doctrine:migrations:<command>.
With this workflow you get easy approach to generic database changes on your application. And you don't need to make any migrations files by hand (just let Doctrine handle those). Although remember to really take a closer look of those generated migration files to make sure that those doesn't contain anything that you really don't want.
Todo
Project contains bunch of tests (Functional, Integration, Unit) which you can run simply by following command:
make run-testsAnd if you want to run tests with fastest library use following command:
make run-tests-fastestNote that you need to create .env.test file to define your testing
environment. This file has the same content as the main .env file, just
change database and others to match your testing environment.
Or you could easily configure your IDE to run these for you.
Project also contains PhpMetrics to make some analyze of your code. You can run this by following command:
make phpmetrics
And after that open build/phpmetrics/index.html with your favorite browser.
- Symfony Flex set to enable RAD (Rapid Application Development)
- Symfony 4: A quick Demo
- Symfony Development using PhpStorm
- Symfony Plugin plugin for PhpStorm
- PHP Annotations plugin for PhpStorm
- Php Inspections (EA Extended) plugin for PhpStorm
- EditorConfig
- composer-version
- Symfony Recipes Server
By default this application uses Argon2 to hash all user passwords, and that is why application requires that your PHP has support for Sodium crypto library (libsodium) library.
If you cannot or don't want to use this as password hashing method you need to change security.yaml before installation step.
To check if your current PHP has libsodium you can use following command
php -mIf [PHP Modules] does not contain libsodium you need to install it to get
password hashing to work. You can install it with following commands on your
*nix box.
git clone -b stable https://github.com/jedisct1/libsodium.git
cd libsodium && sudo ./configure && sudo make check && sudo make install
pecl install libsodiumCopyright (c) 2018 Tarmo Leppänen


