定制 hemend/laravel-api 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hemend/laravel-api

Composer 安装命令:

composer require hemend/laravel-api

包简介

Generate api with service & version & method base

README 文档

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
- database/seeders/DatabaseSeeder.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__;
// });

app()->usePublicPath(__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

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]

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

统计信息

  • 总下载量: 216
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固