PHP code example of yatilabs / laravel-api-access

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

    

yatilabs / laravel-api-access example snippets


## Configuration

After installation, you can customize the package behavior by editing the published configuration file.

### Layout Integration

To integrate the API Access management interface with your existing Laravel application layout:

1. **Set your layout file** in the configuration:
   

// In your routes/api.php or routes/web.php
Route::middleware('api.key')->group(function () {
    Route::get('/protected', function () {
        return 'This is protected!';
    });
});



namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Yatilabs\ApiAccess\Middleware\VerifyApiKey;

class CustomApiMiddleware extends VerifyApiKey
{
    public function handle(Request $request, Closure $next)
    {
        // First verify the API key
        $response = parent::handle($request, $next);
        
        if ($response->getStatusCode() === 401) {
            return $response;
        }
        
        // Get API key model
        $apiKey = $request->attributes->get('api_key_model');
        
        // Add custom checks (e.g., usage limits, specific permissions)
        if ($apiKey->usage_count > 1000) {
            return response()->json([
                'error' => 'Usage limit exceeded',
                'message' => 'API key has exceeded usage limits'
            ], 429);
        }
        
        return $next($request);
    }
}

use Yatilabs\ApiAccess\Services\ApiAccessService;

class YourController extends Controller
{
    protected $apiAccessService;

    public function __construct(ApiAccessService $apiAccessService)
    {
        $this->apiAccessService = $apiAccessService;
    }

    public function createApiKey()
    {
        $apiKey = $this->apiAccessService->createApiKey([
            'description' => 'My API Key',
            'mode' => 'test',
            'is_active' => true
        ]);

        // $apiKey->plain_secret contains the unhashed secret (only available once)
        return $apiKey;
    }
}

'localhost_domains' => [
    'localhost',
    '127.0.0.1',
    '::1',
    '0.0.0.0',
    '*.test',
    '*.local',
    '*.dev',
    'my-custom-dev.domain',
    '*.staging',
],

'model_owner' => [
    'enabled' => true,                     // Enable model owner functionality
    '                   // Primary key column name
    'title_column' => 'name',              // Column to display as owner name
    'label' => 'User',                     // Display label for the model type
    
    // Optional: Additional columns to display in dropdowns
    'additional_columns' => [
        'email',                           // Show email in dropdown
    ],
    
    // Optional: Query constraints for owner selection
    'constraints' => [
        'active' => true,                  // Only show active users
    ],
],

use Yatilabs\ApiAccess\Models\ApiKey;

// Create API key with owner
$apiKey = ApiKey::create([
    'description' => 'Project API Key',
    'mode' => 'live',
    'owner_type' => 'App\\Models\\Project',
    'owner_id' => 123,
    // ... other fields
]);

// Access owner relationship
$owner = $apiKey->owner; // Returns the actual model instance
$ownerName = $apiKey->owner_display_name; // Returns formatted display name
$ownerLabel = $apiKey->owner_label; // Returns the configured label

// Check if owner functionality is enabled
if (ApiKey::isOwnerEnabled()) {
    // Owner functionality is available
}

// Get available owners for selection
$availableOwners = ApiKey::getAvailableOwners();

'model_owner' => [
    'enabled' => true,
    '  'id_column' => 'id',
    'title_column' => 'name',
    'label' => 'Organization',
    'additional_columns' => ['domain'],
    'constraints' => ['status' => 'active'],
],

'model_owner' => [
    'enabled' => true,
    '_column' => 'uuid',
    'title_column' => 'title',
    'label' => 'Project',
    'additional_columns' => ['status', 'client_name'],
],
bash
php artisan vendor:publish --provider="Yatilabs\ApiAccess\ApiAccessServiceProvider" --tag="migrations"
php artisan migrate
bash
php artisan vendor:publish --provider="Yatilabs\ApiAccess\ApiAccessServiceProvider" --tag="assets"
bash
php artisan vendor:publish --provider="Yatilabs\ApiAccess\ApiAccessServiceProvider" --tag="config"
bash
curl "http://your-app.com/api/protected?api_key=your-api-key&api_secret=your-secret"