corsair / m360
M360API PHP 中间件,支持 Symfony 和 Laravel
Requires
- curl/curl: ^2.3
- justinrainbow/json-schema: ^5.2
- zalora/punyan: ^1.0
Requires (Dev)
- phpunit/php-code-coverage: ^9.2
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-29 08:51:57 UTC
README
M360 PHP 中间件是一个依赖项,由使用以下框架构建的 PHP 服务器消费 Symfony 和 Laravel。
该中间件使用原生 PHP 开发,包括方便与上述框架一起使用的接口。
安装
使用 Symfony 安装
使用终端导航到您的 symfony 项目目录,然后执行以下命令
$ cd path_to_my_symfony_project
$ composer require m360.mw.php
然后在编辑器中打开 config/bundles.php 文件,并将中间件包类添加到其中
<?php
return [
...
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
M360\frameworks\Symfony\M360MiddlewareBundle::class => ['all' => true],
...
];
完成了!M360 PHP 中间件现在已安装到您的 symfony 项目中。
使用 Laravel 安装
使用终端导航到您的 laravel 项目目录。编辑您的 composer.json 文件,并在 scripts/post-autoload-dump 下粘贴以下命令
"scripts": {
"post-autoload-dump": [
"@php artisan m360:install"
],
}
然后,在终端中执行以下命令
$ cd path_to_my_laravel_project
$ composer require m360.mw.php
完成了!M360 PHP 中间件现在已安装到您的 laravel 项目中。
配置
现在 M360 中间件已安装,导航到您的项目目录,并为您的微服务创建一个新的合同文件。合同文件是一个简单的 JSON 文件,其中包含您微服务中支持的 API 端点列表。
示例
{
"name": "laravel",
"group": "Testing",
"version": 1,
"ports": {
"data": 443,
"maintenance": 443
},
"apis": {
"main": {
"get": {
"/": {
"access": false,
"label": "List Data Entries"
},
"/:id": {
"access": false,
"label": "Get one Data Entry using its ID"
}
},
"post": {
"/": {
"access": true,
"label": "Create new Data Entry"
}
},
"patch": {
"/:id": {
"access": true,
"label": "Update fields in the Data Entry using its ID"
}
},
"put": {
"/:id": {
"access": true,
"label": "Override the Data Entry using its ID"
}
},
"delete": {
"/:id": {
"access": true,
"label": "Delete the Data entry using its ID"
}
}
},
"maintenance": {
"get": {
"/cache/clear": {
"access": false,
"label": "Clear the Cache of your Microservice"
}
}
}
}
}
您可以在本页面上了解更多关于服务合同的信息 "M360API 中间件知识库"。
symfony 和 laravel 都使用环境文件来设置传递给其应用程序的全局配置值。
使用相同的方法,编辑位于项目目录根目录的 .env 文件,并添加以下变量
Symfony
APP_IP=https://myapi.symfony.local
APP_PLATFORM=manual
M360_SERVICE_CONTRACT=/absolute_path_to_my_symfony_project/contract.json
Laravel
APP_IP=https://myapi.laravel.local
APP_PLATFORM=manual
M360_SERVICE_CONTRACT=/absolute_path_to_my_laravel_project/contract.json
使用
现在中间件已安装,您可以在 API 和控制器中以及 CLI 命令中使用 SDK。
Symfony
API
symfony 使用依赖注入将服务注入到 API 控制器中。以下示例演示了如何使用依赖注入在 symfony 中使用中间件。
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
# add the namespace of the middleware
use M360\frameworks\Symfony\Service\M360MiddlewareService;
class HelloController extends AbstractController
{
# create a private property for the middleware service
private M360MiddlewareService $M360mw;
# declare the middleware service as a dependency
public function __construct (M360MiddlewareService $m360MiddlewareService) {
# assing the middleware service and call the init method
$this->M360mw = $m360MiddlewareService;
$this->M360mw->init();
}
/**
* @Route("/hello", name="hello")
*/
public function hello (): Response {
$response = (object)[];
# you can now use the SDK of the middleware in your code
# this example pulls a copy of the registry settings using the middleware and sets it in the response
$reg = $this->M360mw->SDK->registry->get("registry");
if(!empty($reg)){
$response = $reg;
}
return new Response(json_encode($response, JSON_PRETTY_PRINT), 200, ['Content-Type' => 'application/json']);
}
}
命令
在您的终端中,导航到您的 symfony 项目目录并运行 bin/console
命令。M360 中间件添加的命令将在列表中可用
m360
m360:autoregister Auto Register this laravel server in M360 Gateway
m360:reloadregistry Reloads the Registry of the M360 middleware in this laravel server
Laravel
API
Laravel 支持中间件,这使您可以在请求到达业务逻辑和您的代码之前引导 m360 中间件。因此,使用 m360 中间件在 laravel 中很简单。
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function test(Request $request){
$response = (object)[];
# the middleware is already attached to the Request Object and you don't need to declare anything
# use the SDK of the middleware in your code
# this example pulls a copy of the registry settings using the middleware and sets it in the response
$reg = $request->M360->registry->get("registry");
if(!empty($reg)){
$response = $reg;
}
return response(json_encode($response, JSON_PRETTY_PRINT), 200)->header('Content-Type', 'text/plain');
}
}
命令
在您的终端中,导航到您的 laravel 项目目录并运行 php artisan
命令。M360 中间件添加的命令将在列表中可用
m360
m360:autoregister Auto Register this laravel server in M360 Gateway
m360:install Install the M360 Middleware
m360:reloadregistry Reloads the Registry of the M360 middleware in this laravel server
当您运行 composer require m360.mw.php
时,已触发 m360:install
命令。您不需要再次运行此命令。但是,如果需要重置安装,则该命令是可用的。
命令说明
命令 | 描述 |
---|---|
m360:autoregister | 每次您修改微服务的合同并添加/修改/删除 API 端点时,都应该运行此命令。它将合同更新的副本发送到网关,以便后者保持对您微服务当前支持哪些端点的了解。 |
m360:reloadregistry | 此命令刷新从网关获取的本地缓存的注册表设置副本。每次当前环境的设置更改时,请运行此命令以获取最新更新的版本。 |
容器化部署
当您即将在容器化集群上部署PHP微服务时,行为会有所变化。修改.env文件,并根据您将使用的科技包含以下额外变量。
Docker上的部署
在Docker上部署时,请提供以下额外选项。没有这些选项,中间件与网关之间的握手将失败,以及从控制台触发的任何维护操作。
选项 | 数据类型 | 必填 | 描述 |
---|---|---|---|
platform | 字符串 | 是 | 值等于'docker' |
network | 字符串 | 是 | 值等于连接到该docker服务的docker网络 |
service | 字符串 | 是 | 值等于docker服务的名称 |
containerIP | 字符串 | 否 | 值是docker服务中docker容器的内部IP地址 |
示例
APP_PLATFORM=docker
APP_PLATFORM_OPTIONS_SERVICE=my_docker_service_name
APP_PLATFORM_OPTIONS_NETWORK=my_docker_network_name
Kubernetes上的部署
在Kubernetes上部署时,请提供以下额外选项。没有这些选项,中间件与网关之间的握手将失败,以及从控制台触发的任何维护操作。
选项 | 数据类型 | 必填 | 描述 |
---|---|---|---|
platform | 字符串 | 是 | 值等于'kubernetes' |
namespace | 字符串 | 是 | 值等于您的部署将运行的Kubernetes命名空间 |
service | 字符串 | 是 | 值等于连接到您的部署的Kubernetes服务名称 |
exposedPort | 字符串 | 是 | 值等于暴露的Kubernetes服务端口 |
APP_PLATFORM=kubernetes
APP_PLATFORM_OPTIONS_NAMESPACE=my_kubernetes_namespace_value
APP_PLATFORM_OPTIONS_PORTS=my_kubernetes_service_exposed_port_value
中间件包括如何使用这些服务器进行消费的示例。
这些示例位于此仓库中的frameworks文件夹内。