This project is to help people to start a fast API
PHP, MySQL, Git, Composer, openssl
Symfony has a build-in web server you do not need Apache or Ngnx to run this project. Thanks Symfony :)
Change (my-project-name) with the name of your project
$ composer create-project gasparteixeira/api-boilerplate my-project-name
cd my-project-name
$ php bin/console cache:clear
- Open the file .env and configure your database connection (user, password and database_name)
DATABASE_URL=mysql://user:[email protected]:3306/database_name
- For security reasons, change this password
JWT_PASSPHRASE=boilerplate #change this password name and use the same password when you are generating the ssh keys
- Basic Auth, change username and password
// config/services.yml
parameters:
app_username: boilerplate
app_password: S3cr37W0rd
use the same password you defined in .env, when asked for it
$ mkdir -p config/jwt # For Symfony3+, no need of the -p option
$ openssl genrsa -out config/jwt/private.pem -aes256 4096
$ openssl rsa -pubout -in config/jwt/private.pem -out config/jwt/public.pem
execute the command line to create your database
$ php bin/console doctrine:database:create
make migrations
$ php bin/console make:migration
lets commit the migration (it will create a table called user)
$ php bin/console doctrine:migrations:migrate
add an user (src/DataFixtures/UserFixtures) to make the test
$ php bin/console doctrine:fixtures:load
$ php bin/console server:run
You can test it using Postman or through the terminal with curl using the username and password you defined in config\services.yml
curl -X POST -H "Authorization: Basic $(echo -n [email protected]:12345 | base64)" http://localhost:8000/api/token
Response must start like:
{ "token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE1NTE4MjAwNzEsImVtYWlsIjo.. " }
$ php bin/phpunit
Go to http://localhost:8000/api/doc More stuff check out https://symfony.com