All Projects → tarlepp → symfony-backend

tarlepp / symfony-backend

Licence: MIT license
REST API with Symfony

Programming Languages

PHP
23972 projects - #3 most used programming language
ApacheConf
554 projects

What is this?

MIT licensed Build Status Coverage Status

Simple JSON API which is build on top of Symfony framework.

Table of Contents

Main points

  • This is just an API, nothing else
  • Only JSON responses from API
  • Easy REST API configuration and customization, see examples here and here
  • JWT authentication
  • API documentation

TODO

  • Configuration for each environment and/or developer
  • Authentication via JWT
  • CORS support
  • "Automatic" API doc generation (Swagger)
  • Database connection (Doctrine dbal + orm)
  • Console tools (dbal, migrations, orm)
  • Docker support
  • Logger (monolog)
  • TravisCI tests
  • Make tests, every endpoint
  • Docs - Generic
  • Docs - New api endpoint
  • Docs - New REST service
  • And everything else...

Requirements

  • PHP 7.0+
  • Apache / nginx see configuration information here

Installation

  • Use your favorite IDE and get checkout from git OR just use command git clone https://github.com/tarlepp/symfony-backend.git
  • Open terminal, go to folder where you make that checkout and run following commands

JWT SSH keys generation

$ openssl genrsa -out app/var/jwt/private.pem -aes256 4096
$ openssl rsa -pubout -in app/var/jwt/private.pem -out app/var/jwt/public.pem

Fetch all dependencies

$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar install

Note that this will also ask you some environment settings; db, mail, secrets, jwt, etc.

Environment checks

You need to check that your environment is ready to use this application in CLI and WEB mode. First step is to make sure that var directory permissions are set right. Instructions for this can be found here.

CLI

Open terminal and go to project root directory and run following command.

$ ./bin/symfony_requirements

Check the output from your console.

WEB

Open terminal and go to project root directory and run following command to start standalone server.

$ ./bin/console server:run

Open your favorite browser with http://127.0.0.1:8000/config.php url and check it for any errors. And if you get just blank page double check your permissions.

Apache

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 rewrite

Configuration

Application will ask your configuration settings when you first time run php composer.phar install command. All those parameters that you should change are in /app/config/parameters.yml file, so just open that and made necessary changes to it.

If you want to answer those parameter values again, you can just delete /app/config/parameters.yml file and then run php composer.phar update command.

Database initialization

At start you have just empty database which you have configured in previous topic. To initialize your database just run following command:

$ ./bin/console doctrine:schema:update --force

Creation of user groups and users

First you need to create user groups for your users. You can create new user groups with following command:

$ ./bin/console user:createGroup

And after that you can create new users with following command:

$ ./bin/console user:create

Development

PHP Code Sniffer

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 -i

If you're using PhpStorm following links will help you to get things rolling.

Database changes

Generally you will need to generate migration files from each database change that you're doing. Easiest way to handle these are just following workflow:

  1. Made your changes to Entity (/src/App/Entity/)
  2. Run diff command to create new migration file;
$ ./bin/console doctrine:migrations:diff

With this you won't need to write those migration files by yourself, just let doctrine handle those - although remember to really look what those generated migration files really contains...

Tests

Project contains bunch of tests (unit, functional, integration, etc.) which you can run simply by following commands:

# PHPUnit 
$ ./vendor/bin/phpunit

# PHPSpec
$ ./vendor/bin/phpspec run

Or you could easily configure your IDE to run these for you.

XDebug

Add following lines to your xdebug.ini file to get XDebug work:

xdebug.remote_enable=on
xdebug.remote_autostart=off

Useful resources + tips

Contributing & issues & questions

Please see the CONTRIBUTING.md file for guidelines.

Authors

Tarmo Leppänen

LICENSE

The MIT License (MIT)

Copyright (c) 2017 Tarmo Leppänen

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].