PHP code example of sky337 / secure-api

1. Go to this page and download the library: Download sky337/secure-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/ */

    

sky337 / secure-api example snippets


use Sky337\SecureAPI\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;
}

$user->assignRole('admin');

if ($user->hasRole('admin')) {
    // Logic for admin
}

if ($user->hasAnyRole(['admin', 'editor'])) {
    // Logic for admin or editor
}

use Sky337\SecureAPI\Classes\ApiResponse;

return ApiResponse::success($data, 'User fetched successfully');
return ApiResponse::error('Something went wrong', 400);
return ApiResponse::validationError($validator->errors());

Route::middleware(['auth:api', 'role:admin'])->group(function () {
    Route::get('/dashboard', [AdminController::class, 'index']);
});

Route::middleware(['role:admin,manager'])->get('/reports', [ReportController::class, 'index']);

use Illuminate\Support\Facades\Route;
use Sky337\SecureAPI\Classes\ApiResponse;

Route::post('/login', [AuthController::class, 'login']);

Route::middleware(['auth:api'])->group(function () {
    Route::get('/profile', function() {
        return ApiResponse::success(auth()->user());
    });
});
bash
php artisan vendor:publish --provider="Sky337\SecureAPI\Providers\SecureAPIServiceProvider"
bash
php artisan migrate