domexx / ares
快速便捷的骨架,用于构建您精美的API
1.0.0
2020-11-16 19:57 UTC
Requires
- ext-json: *
- ext-sockets: *
- domexx/ares-core: 3.1.0
This package is auto-updated.
Last update: 2024-09-18 21:41:57 UTC
README
简介
该项目基于微框架slim。它可以用于创建和扩展快速API。
基本功能
- 响应
- 异常
- 验证
- 身份验证
- 区域设置和翻译
- 缓存
- 请求节流
- 数据库
- Illuminate QueryBuilder
- 别名
- 配置
- Dotenv
- 日志
- DI 容器
- 路由(Slim)
安装
1. 克隆/要求项目。
$ git clone https://github.com/Domexx/ares.git or $ composer require domexx/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'); };
创建自定义ServiceProvider
1. 使用扩展AbstractServiceProvider创建新的ServiceProvider
<?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中注册新创建的ServiceProvider
// Adds our CustomProvider to add Customs $container->addServiceProvider( new \Ares\CustomModule\Provider\CustomServiceProvider() );
致谢
如果您有任何问题或反馈,请随时联系我们。
- Discord: Dome#9999
- 邮件: dominik-forschner@web.de
- Discord: s1njar#0066
- 邮件: s1njar.mail@gmail.com
链接
许可证
MIT 许可证(MIT)。