netolabs/php-lambda-router
Composer 安装命令:
composer require netolabs/php-lambda-router
包简介
Route requests to controller actions using the AWS Lambda handler
README 文档
README
Simple handler-based router middleware for the php-lambda-runtime library.
Installation
Run composer require netolabs/php-lambda-router netolabs/php-lambda-runtime from the root of your project.
Using the Router Middleware
The Router middleware is used to route our Lambda to a Controller and Action, much like an MVC framework would. The difference being that instead of routing based on the request path, we use the Lambda handler name. This allows us to have multiple Lambda functions inside the same repository and also allowing more reuse of code.
The handler name is expected in the format of "controller.action". Internally, the router will attempt to load the controller and check if the action method exists. If not, it will pass the request on to the next middleware in the queue; usually a fallback or a 404 middleware.
For example: if the handler name is helloworld.get, the router will attempt to load the \App\Controller\HelloworldController class and execute the method getAction($request).
The default namespace is \App\Controller\, however this is configurable via constructor parameter.
Example
Adding the Router middleware
In this example we're using the Router to route our Lambda to a Controller and Action.
It includes an exception handler and a fallback which returns a 404 response if the route isn't matched.
In your app.php use the following code:
<?php
$app = \Neto\Lambda\Application\AppFactory::create();
$app->addMiddleware(new \Neto\Lambda\Middleware\ExceptionHandler(true))
->addMiddleware(new \Neto\Lambda\Middleware\HandlerRouter(getenv('_HANDLER')))
->addMiddleware(new \Neto\Lambda\Middleware\FileNotFound())
->run();
Creating a Controller
Controllers are autoloaded using the Lambda handler name and must conform to the PSR-4 standard.
mkdir -p src/App/Controller
touch src/App/Controller/AppController.php
src/App/Controller/AppController.php:
<?php
namespace App\Controller;
use GuzzleHttp\Psr7\Response;
use Neto\Lambda\Controller\AbstractController;
class AppController extends AbstractController
{
public function helloworldAction(RequestInterface $request)
{
return new Response(200, [], '<h1>Hello world!</h1>');
}
}
Send it
Now run it using vendor/bin/invoke -h app.helloworld.
You'll notice we are using "app.helloworld" as our handler name - this is used to resolve our controller class
(AppController) and action method (HelloworldAction).
PSR-11 Containers and auto-wiring
Both the Router middleware and the AbstractController implement the ContainerAwareInterface.
If you'd like to use a DI container, you only need to inject it into the HandlerRouter either via constructor or method.
The container object (when set) will also be injected automatically into controllers during instantiation.
Because this library uses PHP-DI's invoker, you can use typed parameters for your action methods in any order
and we will attempt to resolve them in your PSR-11 container using either the parameter type or the parameter name.
License
The MIT License (MIT). Please see the License File for more information.
netolabs/php-lambda-router 适用场景与选型建议
netolabs/php-lambda-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 620 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 08 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 netolabs/php-lambda-router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 netolabs/php-lambda-router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 620
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-27