hemend/laravel-api

Generate api with service & version & method base

v3.3.1 2023-12-04 18:29 UTC

This package is auto-updated.

Last update: 2024-04-09 23:01:14 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}/{package}/{endpoint}', 'Api')->where([
        'service' => '[a-z][a-zA-Z0-9]+',
        'version' => '[1-9][0-9]{0,1}',
        'package' => '[a-z][a-zA-Z0-9]+',
        'endpoint' => '[a-z][a-zA-Z0-9]+'
    ])->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

Keyword Meaning Example
[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:

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]

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

License

Licensed under the MIT license, see LICENSE