Download the PHP package frootbox/rest-api without Composer

On this page you can find all versions of the php package frootbox/rest-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package rest-api

Frootbox REST API

A lightweight, attribute-based REST API framework for PHP.

This package provides a simple way to build versioned REST APIs with support for multiple authentication methods like API keys, Bearer tokens, Basic Auth, and custom client credentials.


โœจ Features


๐Ÿ“ฆ Installation

composer require frootbox/restapi

๐Ÿš€ Getting Started

1. Create a Server instance

use Frootbox\\RestApi\\Server;
use DI\\Container;

$container = new Container();

$server = new Server(
    clientRepository: $clientRepository, // implements ClientRepositoryInterface
    baseUriRegex: '#^/api/v(?P<Version>[0-9]+)(?P<Path>/.*)$#',
    controllerDirectory: __DIR__ . '/Controller',
    namespace: 'App\\\\Controller',
    container: $container,
    hashKey: 'your-secret-key'
);

$server->execute();

๐Ÿ“ Controller Structure

Controllers must follow a versioned namespace structure:

src/
โ””โ”€โ”€ Controller/
    โ””โ”€โ”€ V1/
        โ””โ”€โ”€ UserController.php

๐Ÿงฉ Example Controller

namespace App\\Controller\\V1;

use OpenApi\\Attributes as OA;
use Frootbox\\RestApi\\Attribute\\Auth;
use Frootbox\\RestApi\\Attribute\\ApiKey;
use Frootbox\\RestApi\\Response\\Payload;

class UserController
{
    #[OA\\Get(path: '/users/{int:id}')]
    #[Auth(type: new ApiKey())]
    public function getUser(int $id): Payload
    {
        return new Payload([
            'id' => $id,
            'name' => 'John Doe'
        ]);
    }
}

๐Ÿ” Authentication

You can define one or multiple authentication methods per endpoint:

#[Auth(type: new ApiKey())]
#[Auth(type: new Bearer())]

Supported Auth Methods

API Key

Send via header:

x-api-key: your-api-key

Bearer Token (JWT)

Authorization: Bearer <token>

Basic Auth

Authorization: Basic base64(clientId:clientSecret)

Client Credentials (GET or Basic)

GET /endpoint?client_id=xxx&client_secret=yyy

or via Basic Auth.


๐Ÿง  Client Validation

You must provide a repository implementing:

Frootbox\\RestApi\\Interface\\ClientRepositoryInterface

Example:

class ClientRepository implements ClientRepositoryInterface
{
    public function validate(string $clientId, string $clientSecret): void
    {
        if ($clientId !== 'test' || $clientSecret !== 'secret') {
            throw new \\Exception('Invalid client credentials');
        }
    }

    public function validateApiKey(string $apiKey): void
    {
        if ($apiKey !== 'abc123') {
            throw new \\Exception('Invalid API key');
        }
    }
}

๐Ÿ”„ Versioning

API version is extracted from the URL:

/api/v1/users/1

Your controllers must match the version namespace:

namespace App\\Controller\\V1;

๐Ÿงพ Route Parameters

Integer parameter

/users/{int:id}

String parameter

/users/{slug}

ULID parameter

/users/{ulid:id}

OpenAPI parameter pattern

#[OA\Get(path: '/users/{id}')]
#[OA\Parameter(
    name: 'id',
    in: 'path',
    required: true,
    schema: new OA\Schema(type: 'string', pattern: '^[0-9A-HJKMNP-TV-Z]{26}$'),
)]

Static routes are matched before dynamic routes, so /users/search is preferred over /users/{id} regardless of reflection order.

Parameters are automatically injected into the method.


๐Ÿ“ค Responses

All responses must return:

Frootbox\\RestApi\\Response\\Payload

Example:

return new Payload([
    'success' => true
]);

โš™๏ธ Dependency Injection

Controllers are resolved via the provided DI container:

$container->call([$controller, $method]);

You can inject services directly into controller methods:

public function getUser(UserService $service, int $id)

๐Ÿ”ง Hooks

Token decoding hook

$server = new Server(..., onDecodeToken: function ($token) {
    // custom logic
});

Client validation hook

$server = new Server(..., onValidateClient: function ($clientId) {
    // custom logic
});

โš ๏ธ Important Notes


๐Ÿ“„ License

MIT """


All versions of rest-api with dependencies

PHP Build Version
Package Version
Requires php Version >=8.3
zircote/swagger-php Version ^4.10
php-di/php-di Version ^6.3
firebase/php-jwt Version ^7
frootbox/exceptions Version ^0.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package frootbox/rest-api contains the following files

Loading the files please wait ...