LSP merchant API

Keywords: Programming git PHP SSL Nginx

Your domain:                  lsp-api-merchant.hhs2717.cn
Virtualhost conf:             /usr/local/nginx/conf/vhost/lsp-api-merchant.hhs2717.cn.conf
Directory of:                 /data/wwwroot/lsp-api-merchant.hhs2717.cn
Rewrite rule:                 /usr/local/nginx/conf/rewrite/laravel.conf
Self-signed SSL Certificate:  /usr/local/nginx/conf/ssl/lsp-api-merchant.hhs2717.cn.crt
SSL Private Key:              /usr/local/nginx/conf/ssl/lsp-api-merchant.hhs2717.cn.key
SSL CSR File:                 /usr/local/nginx/conf/ssl/lsp-api-merchant.hhs2717.cn.csr
scp -P 22 -r /Users/beanho/Downloads/cert-1542330026815_lsp-api-merchant.hhs2717.cn_nginx/* root@39.108.126.147:/usr/local/nginx/conf/ssl/

Create a new Laravel project and synchronize git

$ valet restart
$ cd ~/Sites
$ laravel new Lsp-api-merchant

Visit http://Lsp-api-merchant.test/

$ cd ~/Sites/Lsp-api-merchant
$ git init
$ git checkout -b api-merchant
$ git add . && git commit -m "Initial commit"
$ git remote add origin git@git.coding.net:qbgugu/Lsp.git
$ git pull origin api-merchant --allow-unrelated-histories
$ git push -u origin api-merchant

Modify time zone config/app.php

'timezone' => 'PRC',

Install dingo API and configure

Modify composer.json

.
.
"require": {
    "php": "^7.1.3",
    "dingo/api": "2.0.0-alpha1",
    "fideloper/proxy": "^4.0",
    "laravel/framework": "5.5.*",
    "laravel/tinker": "^1.0"
}
.
.
$ composer update
$ php artisan vendor:publish

Modify.env

.
.
# dingo config
API_STANDARDS_TREE=prs
API_SUBTYPE=lsp
API_PREFIX=api
# API_DOMAIN=api-user.lsp.com
API_VERSION=v1
API_DEBUG=true

Modify user table structure

Modify AppServiceProvider.php to manually configure the string length generated by migration

.
.
public function boot()
{
    Schema::defaultStringLength(191);
}
.
.

To modify data table fields, you need the doctrine/dbal component

$ composer require doctrine/dbal

New base class

$ php artisan make:controller Api/V1/Controller
<?php

namespace App\Http\Controllers\Api\V1;

use Dingo\Api\Routing\Helpers;
use App\Http\Controllers\Controller as BaseController;

class Controller extends BaseController
{
    use Helpers;
}

Sql log: query log component written with security

composer require overtrue/laravel-query-logger --dev

Create your own helper functions

touch app/helpers.php

composer.json

"autoload": {
        "files": [
            "app/helpers.php"
        ],
.
.
composer dump-autoload

API exception handling

app/Providers/AppServiceProvider.php

\API::error(function (\Illuminate\Database\Eloquent\ModelNotFoundException $exception) {
    abort(404);
});
\API::error(function (\Illuminate\Auth\Access\AuthorizationException $exception) {
    abort(403, $exception->getMessage());
});

Add code error code field to error response

app/Http/Controllers/Api/Controller.php

/**
 * Erroneous return
 *
 * @param $statusCode   HTTP Status code
 * @param null $message Error message
 * @param int $code     Error code
 */
public function errorResponse($statusCode, $message=null, $code=0)
{
    throw new HttpException($statusCode, $message, null, [], $code);
}

Unified return

Single resource does not need data Key for package

composer require liyu/dingo-serializer-switch

Posted by spoons84 on Sat, 07 Dec 2019 06:12:50 -0800