PHPackages                             hemend/laravel-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. hemend/laravel-api

ActiveLibrary[API Development](/categories/api)

hemend/laravel-api
==================

Generate api with service &amp; version &amp; method base

v5.1.2(7mo ago)0216MITPHPPHP &gt;=8.1

Since Apr 7Pushed 7mo ago2 watchersCompare

[ Source](https://github.com/HemendCo/laravel-api)[ Packagist](https://packagist.org/packages/hemend/laravel-api)[ RSS](/packages/hemend-laravel-api/feed)WikiDiscussions main Synced 4w ago

READMEChangelog (10)Dependencies (4)Versions (44)Used By (0)

laravel-api
===========

[](#laravel-api)

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](https://camo.githubusercontent.com/384e14766a972fa9f8b3c360af881c1b113233f27592f238a8a581233e54505c/687474703a2f2f706f7365722e707567782e6f72672f68656d656e642f6c61726176656c2d6170692f76)](https://packagist.org/packages/hemend/laravel-api)[![Total Downloads](https://camo.githubusercontent.com/037fc28070e4e975448bd1e4025c40c3924bdd922ea854e8a8a216ee8d292215/687474703a2f2f706f7365722e707567782e6f72672f68656d656e642f6c61726176656c2d6170692f646f776e6c6f616473)](https://packagist.org/packages/hemend/laravel-api)[![Latest Unstable Version](https://camo.githubusercontent.com/9a3a38951b2f88936813236df691c563d5c8d09c0c8c871d04489ae0dcbfbb22/687474703a2f2f706f7365722e707567782e6f72672f68656d656e642f6c61726176656c2d6170692f762f756e737461626c65)](https://packagist.org/packages/hemend/laravel-api)[![License](https://camo.githubusercontent.com/3043e4e3087d85cbea5ec2cbb9302ac09a83fb94e5395959729c2895f502e6ff/687474703a2f2f706f7365722e707567782e6f72672f68656d656e642f6c61726176656c2d6170692f6c6963656e7365)](https://packagist.org/packages/hemend/laravel-api)[![PHP Version Require](https://camo.githubusercontent.com/dec3c92f1e1333cee631f59f6d65bc3b50ad784beeb763535b1e56b9cf323283/687474703a2f2f706f7365722e707567782e6f72672f68656d656e642f6c61726176656c2d6170692f726571756972652f706870)](https://packagist.org/packages/hemend/laravel-api)

Requirements
------------

[](#requirements)

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

[](#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
- database/seeders/DatabaseSeeder.php

```

#### Publish commands

[](#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

[](#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__;
// });

app()->usePublicPath(__DIR__);

$kernel = $app->make(Kernel::class);
```

2. Edit `config/auth.php`:

```
    ...
    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],
    ...
    'guards' => [
    ...
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
    ...
    ],
    ...
    'providers' => [
    ...
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\Users::class,
        ],
    ...
    ],
```

3. 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
------------

[](#api-commands)

#### Keyword meanings

[](#keyword-meanings)

KeywordMeaningExample\[Service\]Service name`Admin` or `Client` ...\[Version\]Version of service`1` or `2` or ... or `99`\[Package\]Package from the server version`Auth` or `Account` ...\[Endpoint\]Endpoint from the server version`SignIn` or `TokensGet` ...\[Mode\]?Set the endpoint mode default(`client`)`admin` or `client`\[Guard\]?Set the service guard default(`api`)`admin` or `client`\[Flag\]?Set the permission flag default(`private`)`private` or `public` or `private_only` or `public_only`#### Create a service with default endpoints:

[](#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)

[](#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)

[](#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:

[](#create-a-service)

```
php artisan make:api-service [Service]
```

#### Create a version for service:

[](#create-a-version-for-service)

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

#### Collecting endpoints and storing them in the database

[](#collecting-endpoints-and-storing-them-in-the-database)

```
php artisan api:acl-collect [Service]
```

Other settings
--------------

[](#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
```

2. Trackable Job Example(path: app/Jobs/TrackableTest.php):

```
prepareTracker();
    }

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

        for ($i = 0; $i setProgressNow($i);
        }

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

usage:

```
