xervice/api

2.0.0 2018-08-27 10:16 UTC

This package is auto-updated.

Last update: 2024-03-29 03:32:31 UTC


README

Scrutinizer Code Quality Build Status.svg?branch=master) Code Coverage

Installation

composer require xervice/api

Configuration

If you want to use secured api controller, you have to add the kernel service and the authenticator to the security module:

  • You must add the "\Xervice\Api\Business\Plugin\ApiAuthService" plugin to the kernel stack.
  • Also you must define your authoration types and add their authenticator (\Xervice\Api\Business\Model\Authenticator\ApiAuthenticator) to the security dependency provider.

Using

To use an api controller without authentification, you can extend from AbstractApiController.

<?php


namespace App\MyModule\Communication\Controller;

use \DataProvider\MyDataDataProvider;
use Symfony\Component\HttpFoundation\Response;
use Xervice\Api\Communication\Controller\AbstractApiController;

class MyApiController extends AbstractApiController
{
    /**
     * @param \DataProvider\MyDataDataProvider $dataProvider
     * @param string $name
     *
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws \Xervice\Api\Exception\ApiException
     */
    public function myRequestAction(MyDataDataProvider $dataProvider, string $name): Response
    {
        $dataProvider = $this->getFacade()->doAnythingWithData($dataProvider);

        return $this->apiResponse($dataProvider);
    }
}

If you want to have authentication, you can extend from AbstractSecureApiController. Every call to an SecureController check an authorization before running the action. The authentication data comes from the header HTTP_Authorization.

Example

HTTP_Authorization: Token Zm9vOmJhcg==

In that example it will look for an security authenticator "Token" and run the authentification. On failure, it will throw an AuthorizationException.