reinvanoyen/dry-internal-api

此包最新版本(1.0.1)没有可用的许可证信息。

DRY应用的内部API

1.0.1 2019-10-10 08:13 UTC

This package is auto-updated.

Last update: 2024-09-10 18:56:03 UTC


README

DRY应用的内部API

安装

composer require reinvanoyen/dry-internal-api

示例用法

路由定义
<?php

use Tnt\InternalApi\Facade\Api;

Api::get('posts/', '\\Acme\\Controller\\PostController::index');
Api::post('posts/', '\\Acme\\Controller\\PostController::add');
Api::delete('posts/(?<postId>\d+)/', '\\Acme\\Controller\\PostController::delete');
控制器
<?php

namespace Acme\Controller;

use Tnt\InternalApi\Exception\ApiException;
use Tnt\InternalApi\Http\Request;

class PostController
{
    public static function index(Request $request)
    {
        return [
            [
                'id' => 1,
                'title' => 'My example post',
            ],
            [
                'id' => 2,
                'title' => 'Another example post',
            ],
        ];
    }
    
    public static function add(Request $request)
    {
        // Create your post
    }
    
    public static function delete(Request $request)
    {
        if ($request->data->integer('postId')) {
            // Delete your post
            return true;
        }
        throw new ApiException('post_not_found');
    }
}