API Bundle

 

  1. To Create an API Install VankoSoft API Bundle:
    composer require vankosoft/api-bundle
  2. Load Bundle and Dependencies in config/admin-panel/bundles.php
    <?php
    
    return [
        ---
        
        ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle::class => ['all' => true],
        Vankosoft\ApiBundle\VSApiBundle::class => ['all' => true],
        Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle::class => ['all' => true],
        Nelmio\CorsBundle\NelmioCorsBundle::class => ['all' => true],
        
        ---
    ];
    
  3. Add Bundle Configuration: config/admin-panel/packages/vs_api.yaml
    imports:
        - { resource: "@VSApiBundle/Resources/config/app/api_platform.yaml" }
        - { resource: "@VSApiBundle/Resources/config/app/serializer.yaml" }
        - { resource: "@VSApiBundle/Resources/config/app/lexik_jwt_authentication.yaml" }
        - { resource: "@VSApiBundle/Resources/config/app/nelmio_cors.yaml" }
    
    
    vs_api:
        enabled: true
    
    
    
  4. Add a Routes configuration in config/admin-panel/routes/vs_api.yaml
    vs_api_platform:
        resource: "@VSApiBundle/Resources/config/routing/api_platform.yaml"
        prefix:   /
    
    vs_api_users:
        resource: "@VSApiBundle/Resources/config/routing/api.yaml"
        prefix:   /
    
    vs_api_test_index:
        path: /api/test-index
        controller: App\Controller\Api\TestController::index
    
  5. For Using the API Routes you need to generate JWT keys at first with this command:
    bin/console lexik:jwt:generate-keypair
    

    The keys will be generated in <project_dir>/config/jwt/

  6. To Authenticate to API create a POST Request to: http://admin.test-vankosoft-application.lh/api/login_check with body:
    {
        "username":"admin",
        "password":"admin"
    }

    The returned response is something like:

    {
        "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE2NjY4NzkxOTcsImV4cCI6MTY2Njg4Mjc5Nywicm9sZXMiOlsiUk9MRV9TVVBFUl9BRE1JTiJdLCJ1c2VybmFtZSI6ImFkbWluIn0.Iu6tRGdXD4PfG08t62TK_oW32lfUd2MPzZ6ygpD7q5me7qvqnjGosU9xfI5ygE34LAhk2KWGG7Jgww0h9iL_3nbjMWN3lzhkVBYhZSbs7bXuz914moSz7_eYPJqKrGpklHVKWAXz6ksFaz2TxW34jR2xMEjoYGYmB_4S03CpsbpTBFHvmxfNym_NTy9ye7xDFcPZg3elQHa2J3r0llgnMjenyonFAvoMSHE8d71cyLDLOBci4juGl2P4oLO0ZR2xcw3qVThsVfGxz3VcdTYH730_kvtI5YIwhAViTAzFunvOz5NpURhLFXn9xCkISi5EVisp5SRxhOqFo_CsMIWYrg"
    }
  7. Then You can get the logged User Info with making a GET Request to: :http://admin.test-vankosoft-application.lh/api/logged-user with header: Authorization: Bearer {token} and returned response is something like:
    {
        "status": "ok",
        "data": {
            "tokenCreatedTimestamp": 1666879197,
            "tokenExpiredTimestamp": 1666882797,
            "user": {
                "username": "admin",
                "email": "admin@test-vankosoft-application.lh",
                "firstName": "Super",
                "lastName": "Admin"
            }
        }
    }
  8. Api Documentation will be auto generated.