快速便捷的骨架,构建您美丽的API

1.0.0 2020-12-31 21:01 UTC

This package is auto-updated.

Last update: 2024-09-29 05:56:16 UTC


README

CodeFactor GitHub license

简介

本项目基于微框架slim。可用于创建和扩展快速API。

基本功能

  • 响应
  • 异常
  • 验证
  • 认证
  • 地区和翻译
  • 缓存
  • 请求节流
  • 数据库
  • Illuminate QueryBuilder
  • 别名
  • 配置
  • Dotenv
  • 日志
  • 依赖注入容器
  • 路由(Slim)

安装

1. 克隆/要求项目。
$ git clone https://github.com/Ares-Legacy/ares.git
or
$ composer require ares-legacy/ares
2. 切换到项目目录。
$ cd ares
3. 复制dotenv配置。
$ cp .env.example .env 
4. 安装composer依赖。
$ composer install 
5. 给文件夹赋予权限。
$ chmod -R 775 {dir name} 

配置

配置您的.env
# Database Credentials
DB_HOST=db
DB_PORT=db
DB_NAME="ares"
DB_USER="ares"
DB_PASSWORD="your_password"

# development / production
API_DEBUG="production"

WEB_NAME="Ares"
# Defines the link to your frontend application // For CORS purpose (*) to allow all
WEB_FRONTEND_LINK="*"

# 1 = Enabled, 0 = Disabled
CACHE_ENABLED=1
# "Files" for Filecache, "Predis" for Redis
CACHE_TYPE="Files"
# Defines the cache alive time in seconds
CACHE_TTL=950

# Redis Host
CACHE_REDIS_HOST=127.0.0.1
# Redis Port, standard Port is 6379
CACHE_REDIS_PORT=6379

# Only works with Redis // Enables Throttling // Enables that people only can call the endpoint certain times in a short period
THROTTLE_ENABLED=0

TOKEN_ISSUER="Ares-Issuer"
TOKEN_DURATION=86400

# The secret must be at least 12 characters in length; contain numbers; upper and lowercase letters; and one of the following special characters *&!@%^#$
TOKEN_SECRET="Secret123!456$"

扩展项目

创建自定义模块

1. 创建继承BaseController的控制器类

在控制器类中,您可以定义由调用路由时调用的函数,这些路由可以在app/routes/routes.php中定义。通过在路由路径和路由操作调用之间设置中间件'AuthMiddleware',可以使用受保护的认证路由。

<?php

namespace Ares\CustomModule\Controller;

use Ares\Framework\Controller\BaseController;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

/**
 * Class Controller
 *
 * @package Ares\CustomModule\Controller
 */
class Controller extends BaseController
{
    /**
     * Reveals a custom response to the user
     *
     * @param Request  $request  The current incoming Request
     * @param Response $response The current Response
     *
     * @return Response Returns a Response with the given Data
     */
    public function customResponse(Request $request, Response $response): Response
    {
        /** @var string $customResponse */
        $customResponse = 'your custom response';

        return $this->respond(
            $response,
            response()
                ->setData($customResponse)
        );
    }
}
2. 在app/routes.php中注册自定义路由,调用控制器函数
return function (App $app) {
    // Registers our custom routes that calls the customResponse function in our custom controller
    $app->get('/custom', \Ares\CustomModule\Controller\Controller::class . ':customResponse');
};

创建自定义服务提供者

1. 创建新的服务提供者,扩展AbstractServiceProvider
<?php

namespace Ares\CustomModule\Provider;

use Ares\CustomModule\Model\Custom;
use League\Container\ServiceProvider\AbstractServiceProvider;

/**
 * Class CustomServiceProvider
 *
 * @package Ares\CustomModule\Provider
 */
class CustomServiceProvider extends AbstractServiceProvider
{
    /**
     * @var array
     */
    protected $provides = [
        Custom::class
    ];

    /**
     * Registers new service.
     */
    public function register()
    {
        $container = $this->getContainer();

        $container->add(Custom::class, function () {
            return new Custom();
        });
    }
}
2. 在app/etc/providers.php中注册新创建的服务提供者
    // Adds our CustomProvider to add Customs
    $container->addServiceProvider(
        new \Ares\CustomModule\Provider\CustomServiceProvider()
    );

致谢

如果您有任何问题或反馈,请随时联系我们。

链接

许可证

MIT许可证(MIT)。