PHP code example of api-skeletons / laravel-doctrine-apikey

1. Go to this page and download the library: Download api-skeletons/laravel-doctrine-apikey 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/ */

    

api-skeletons / laravel-doctrine-apikey example snippets


    'providers' => [
        ...
        ApiSkeletons\Laravel\Doctrine\ApiKey\ServiceProvider::class,
    ],

use ApiSkeletons\Laravel\Doctrine\ApiKey\Http\Middleware\AuthorizeApiKey;

$routeMiddleware = [
    ...
    'auth.apikey' => AuthorizeApiKey:class
];

use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;

public function boot()
{
    app(ApiKeyService::class)->init(app('em'));
}

Route::name('api.resource::fetch')
    ->get('resource', 'ResourceController::fetch')
    ->middleware('auth.apikey');

Route::name('api.resource::fetch')
    ->get('resource', 'ResourceController::fetch')
    ->middleware('auth.apikey:{scopeName}');

$apiKey = request()->attributes->get('apikey');



declare(strict_types=1);

namespace App\ORM\Event\Subscriber;

use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey;
use App\ORM\Entity\Customer;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;

class ApiKeyEventSubscriber implements
    EventSubscriber
{
    /**
     * {@inheritDoc}
     */
    public function getSubscribedEvents()
    {
        return [
            Events::loadClassMetadata,
        ];
    }

    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
    {
        // the $metadata is the whole mapping info for this class
        $metadata = $eventArgs->getClassMetadata();

        switch ($metadata->getName()) {
            case Customer::class:
                $metadata->mapOneToOne([
                    'targetEntity' => ApiKey::class,
                    'fieldName' => 'apiKey',
                ]);
                break;
            default:
                break;
        }
    }
}

use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;

public function register(): void
{
    $this->app->singleton('ApiKeyService2', static function ($app) {
        return new ApiKeyService();
    });
}

public function boot()
{
    app('ApiKeyService2')->init(app('em2'));
}

$routeMiddleware = [
    ...
    'auth.apikey2' => EditedAuthorizeApiKey:class
];
shell
$ php artisan apikey:generate yourapikeyname
shell
php artisan apikey:scope:generate {name}
shell
php artisan apikey:generate {name}
shell
php artisan apikey:scope:generate {name}
shell
php artisan apikey:scope:add {apiKeyName} {scopeName}
shell
php artisan apikey:deactivate {name}
shell
php artisan apikey:activate {name}
shell
php artisan apikey:scope:remove {apiKeyName} {scopeName}
shell
php artisan apikey:regenerate {name}
shell
php artisan apikey:scope:delete {scopeName}
shell
php artisan apikey:print {name?} 
shell
php artisan apikey:scope:print {name?}