hemend/laravel-api

Generate api with service & version & method base

v4.7.1 2024-11-22 10:09 UTC

README

Use shields for your packagist.org repository that shows how many times your project has been downloaded from packagist.org or its latest stable version.

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Requirements

It is mandatory to delete files whose path is listed below:

- app/Models/User.php
- database/migrations/2014_10_12_000000_create_users_table.php
- database/migrations/2014_10_12_100000_create_password_reset_tokens_table.php

Publish commands

In this section, you need to copy the required files from the package to your local path. If you execute the following command, you do not need to use commands after that:

php artisan vendor:publish --provider="Hemend\Api\ApiServiceProvider" --tag=api
php artisan vendor:publish --provider="Hemend\Library\Laravel\Providers\LibraryServiceProvider" --tag=config
Copy config

php artisan vendor:publish --provider="Hemend\Api\ApiServiceProvider" --tag=config

Copy migrations

php artisan vendor:publish --provider="Hemend\Api\ApiServiceProvider" --tag=migrations

Copy seeders

php artisan vendor:publish --provider="Hemend\Api\ApiServiceProvider" --tag=seeders

Copy models

php artisan vendor:publish --provider="Hemend\Api\ApiServiceProvider" --tag=models

Changes in project files

  1. Edit public/index.php:
$app = require_once __DIR__.'/../bootstrap/app.php';

// set the public path to this directory
$app->bind('path.public', function() {
    return __DIR__;
});

$kernel = $app->make(Kernel::class);
  1. Edit config/auth.php:
    ...
    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],
    ...
    'guards' => [
    ...
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
    ...
    ],
    ...
    'providers' => [
    ...
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\Users::class,
        ],
    ...
    ],
  1. Empty the contents of the routes/api.php file and paste the following codes:
function callApiRoute($route_name) {
    Route::any('/{service}/{version}/{endpoint}', 'Api')->where([
        'service' => '[a-z][a-zA-Z0-9]+',
        'version' => '[1-9][0-9]{0,1}',
        'endpoint' => '([a-z][a-zA-Z0-9]+(\/?[a-z][a-zA-Z0-9]+){0,6})'
    ])->name($route_name);
}

Route::group(['namespace' => 'Hemend\Api\Controllers\\'], function ($router) {
    callApiRoute('Api');

//    Route::group(['prefix' => 'demo'], function ($router) {
//        callApiRoute('DemoApi');
//    });
});

Api commands

Keyword meanings

Create a service with default endpoints:

php artisan make:api-basic [Service] [Version] --mode=[Mode] --guard=[Guard]

Create a specific endpoint (It is created if there is no service and version)

php artisan make:api-maker [Service] [Version] [Package] [Endpoint] --flag=[Flag]

Create a specific endpoint (You will get an error if there is no service and version)

php artisan make:api-endpoint [Service] [Version] [Package] [Endpoint] --flag=[Flag]

Create a service:

php artisan make:api-service [Service]

Create a version for service:

php artisan make:api-version [Service] [Version]

Collecting endpoints and storing them in the database

php artisan api:acl-collect [Service]

Other settings

  1. After installing the package and doing the above, you need to publish and migrate to the database:
php artisan migrate
php artisan passport:install
php artisan db:seed --class=UsersSeeder
  1. Trackable Job Example(path: app/Jobs/TrackableTest.php):
<?php

namespace App\Jobs;

use Hemend\Api\Interfaces\TrackableJob;
use Hemend\Api\Traits\TrackableQueue;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class TrackableTest implements ShouldQueue, TrackableJob
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, TrackableQueue;

    /**
     * Create a new job instance.
     */
    public function __construct()
    {
        $this->prepareTracker();
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $max = mt_rand(5, 30);
        $this->setProgressMax($max);

        for ($i = 0; $i <= $max; $i += 1) {
            sleep(1); // Some Long Operations
            $this->setProgressNow($i);
        }

        $this->setOutput(['total' => $max, 'other' => 'parameter']);
    }
}

usage:

<?php

use App\Jobs\TrackableTest;

TrackableTest::dispatch();

License

Licensed under the MIT license, see LICENSE