PHP code example of tetes-de-pioche / laravel-modular-api

1. Go to this page and download the library: Download tetes-de-pioche/laravel-modular-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tetes-de-pioche / laravel-modular-api example snippets


return [

    /*
    |--------------------------------------------------------------------------
    | API configuration
    |--------------------------------------------------------------------------
    */

    'api' => [

        'routing' => [
            'url' => env('MODULAR_API_ROUTING_URL', env('APP_URL', 'http://localhost')),
            'url_prefix' => env('MODULAR_API_ROUTING_URL_PREFIX', '/'),
            'route_prefix' => env('MODULAR_API_ROUTING_ROUTE_PREFIX', 'api'),
            'enable_version_prefix' => env('MODULAR_API_ROUTING_ENABLE_VERSION_PREFIX', true),
            'enable_type_prefix' => env('MODULAR_API_ROUTING_ENABLE_TYPE_PREFIX', true),
        ],

        'resource' => [
            'custom_type_resolver' => env('MODULAR_API_RESOURCE_CUSTOM_TYPE_RESOLVER', false),
        ],

        'pagination' => [
            'size_default' => env('MODULAR_API_PAGINATION_SIZE_DEFAULT', 25),
            'size_max' => env('MODULAR_API_PAGINATION_SIZE_MAX', 100),
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Web configuration
    |--------------------------------------------------------------------------
    */

    'web' => [

        'routing' => [
            'url' => env('MODULAR_API_WEB_ROUTING_URL', env('APP_URL', 'http://localhost')),
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Modular code configuration
    |--------------------------------------------------------------------------
    */

    'services' => [

        'root_path' => env('MODULAR_API_SERVICES_ROOT_PATH', 'Services'),

    ],

    /*
    |--------------------------------------------------------------------------
    | Features configuration
    |--------------------------------------------------------------------------
    */

    'features' => [

        'rate_limiting' => [
            'enabled' => env('MODULAR_API_FEATURE_RATE_LIMITING_ENABLED', false),
            'attempts' => env('MODULAR_API_FEATURE_RATE_LIMITING_ATTEMPTS_PER_MIN', 30),
            'expires' => env('MODULAR_API_FEATURE_RATE_LIMITING_EXPIRES_IN_MIN', 1),
        ],

        'localization' => [
            'enabled' => env('MODULAR_API_FEATURE_LOCALIZATION_ENABLED', false),
            'request_header' => env('MODULAR_API_FEATURE_LOCALIZATION_REQUEST_HEADER', 'Accept-Language'),
            'locales' => env('MODULAR_API_FEATURE_LOCALIZATION_LOCALES', env('APP_LOCALE', 'en')),
        ],

        'obfuscated_ids' => [
            'enabled' => env('MODULAR_API_FEATURE_OBFUSCATED_IDS_ENABLED', false),
            'key' => env('MODULAR_API_FEATURE_OBFUSCATED_IDS_KEY', env('APP_KEY')),
            'min_length' => env('MODULAR_API_FEATURE_OBFUSCATED_IDS_MIN_LENGTH', 20),
            'alphabet' => env('MODULAR_API_FEATURE_OBFUSCATED_IDS_ALPHABET',
                'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'),
        ],

    ],

];

App/
    Services/
        DomainA/
            Service1/
                Config/
                Data/
                    Migrations/
                Http/
                    Controllers/
                    Endpoints/
                    Requests/
                    WebEndpoints/
                Mails/
                    Templates/
                Models/
                Resources/
                Providers/
                    MainServiceProvider.php
                Views/
            Service2/
                ...
        DomainB/
            Service1/
                ...
            Service2/
                ...

// page[number] / page[size], other query parameters preserved in the links
$paginator = MyModel::query()->jsonApiPaginate($request);

// resolve one record from the identifier as exposed by the API
$myModel = MyModel::query()->jsonApiFind($id);

MyModel::query()->jsonApiPaginate($request, perPage: 25, maxPerPage: 500);

class MyResource extends ApiResource
{
    public static bool $useObfuscatedIds = false;
}

$this->app->bind(ObfuscatedIdEncoder::class, MyEncoder::class);
bash
php artisan vendor:publish --tag="modular-api-config"