MustafaMagdi / Symfony4

Symfnoy4 boilerplate using Flex with ReactPHP supported.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Symfony4

Symfnoy4 using Flex with ReactPHP supported.

Purpose

We didn't re-invent the wheel here, we are just coming up with the latest updates from Symfony4 with some customizations to make it fit your usage in your microservices architecture.

What Do We Have

  • Symfony with Flex module.
  • Add ReactPHP.
  • Restful support (Simple).
  • Custom ControllerTrait.
  • Middleware.
  • CORS NelmioCorsBundle.
  • GWT LexikJWT.
  • Add Debug via parameters (like sending _profile as a param).
  • Basic Authentication.
  • Basic AccessControl.
  • Unify the response body/add a pattern.
  • Add ExceptionalTrait.
  • Use PSR-7 Bridge with ReactPHP.
  • Clean the code.
  • Integrate blackfire.io.
  • Add tests.

Documentation

  • Why.
  • Why Symfony.
  • How to setup.
  • Run the app.
  • Installing new packages.
  • Profiling.
  • How to integrate JWT with Symfony security system.
  • How to create a new endpoint.
  • References for Symfony4.

Why

Add the minimal amount of components and services that serve the needs of having Restful API following the standards.

Why Symfony

Wait for it.

How to setup

  1. Clone the repo:
git clone https://github.com/MustafaMagdi/Symfony4.git
  1. Install packages:
cd Symfony4 && composer install
  1. Configure .env file:
cp .env.dist .env
  1. Generate SSH keys for LexikJWT:
$ mkdir -p var/jwt
$ openssl genrsa -out var/jwt/private.pem -aes256 4096
$ openssl rsa -pubout -in var/jwt/private.pem -out var/jwt/public.pem

It might asks you for passphrase, provide it and add it to your .env file:

JWT_PASSPHRASE=whatever
  1. Configure NelmioCorsBundle, check your config/packages/nelmio_cors.yaml file.

And now, you run the application:

Run the app

  1. For the regular setup:
./bin/console server:run
  1. For ReactPHP:
php public/index.react.php

Installing new services

Before you start searching in Packagist, have a look at Symfony.sh which is a hub for Symfony services configuration. It is playing with Symfony flex and Symfony recipes a really nice role in Symfony4.

I am going to provide more details about it soon.

Profiling

Symfony is already coming up with a really nice Profiler Component, to see the nice profiler bar in browser, just send _profiler param in your request, it even dumps the response body.

On development env, you still can open the profiler url form the response header, just check the X-Debug-Token-Link.

How to integrate JWT with Symfony security system

In order to understand how Symfony security system is working, check the documentation here. We have two steps to configure JWT with Symfony security system:

One second, if you are not aware about JWT, just check jwt.io.

1. Login the user:

You need to generate the token for the user:

Controller/AuthController.php:generateToken
2. Validate the user:

By defining the authenticator class in config/packages/security.yaml:

        guard:
            authenticators:
                - App\Security\JwtTokenAuthenticator

Check App\Security\JwtTokenAuthenticator:getUser, which you decode the token and match with your DB user record, then, you return the Symfony\Component\Security\Core\User object with a dummy user role that should match what we have in security.yaml:

    access_control:
        - { path: ^/profile, roles: ROLE_USER }

How to create a new endpoint

It is very simple, you will notice at the docblock of the controller functions:

    /**
     * @Route("/auth") // where you provide the route
     * @Method({"POST"}) // where you provide the HTTP method
     * ...
     */

References for Symfony4

In order to involve in Symfony:

  1. For sure, it is Symfony's website.
  2. Symfony's blog, they have "A week of Symfony" is being release in a weekly basis with all the updates.
  3. KNP Symfony there are some courses introduced by weaverryan, I really like the way he is introducing them. Thanks you @weaverryan.

About

Symfnoy4 boilerplate using Flex with ReactPHP supported.

License:MIT License


Languages

Language:PHP 98.9%Language:HTML 1.1%