willitscale/streetlamp
Composer 安装命令:
composer require willitscale/streetlamp
包简介
A HTTP router application wrapper.
README 文档
README
Table of Contents
- 1. Introduction
- 2. Prerequisites
- 3. Setup
- 3.1. Installing the library
- 3.2. Application Wrapper
- 3.3 Creating a Controller
- 3.4. Creating a Route
- 4. PSR Compliance
- 5. Further Reading
1. Introduction
Streetlamp is a simple routing library that allows you to quickly prototype APIs. This library was built around the basic concepts of annotative routing, commonly found in Java libraries such as JAX-RS and Spring. Although the way it works is inspired from the aforementioned Java libraries, it has a slightly unique implementation more fitting the PHP language.
2. Prerequisites
To keep up with modern standards this library was built using PHP 8.4 and therefore will only run in said environment or greater. Due to the speed of which PHP is evolving, it is recommended to always use the latest stable version of PHP. Finally, this project requires composer and the PSR-4 Autoload standard.
3. Setup
3.1. Installing the library
Simply include Streetlamp in your project with the composer command:
composer require willitscale/streetlamp
3.2. Application Wrapper
To run your application through the Streetlamp wrapper all you need to do is instantiate the Router class and call
route.
The Router will use a RouteBuilder to scan all of your namespaces in the composer.json (excluding test namespaces
by default) and setup corresponding routes.
Here's all the code you need to get going:
<?php declare(strict_types=1); require_once 'vendor/autoload.php'; use willitscale\Streetlamp\Router; new Router()->route();
This will use a simple out of the box configuration, if you require any customisation this can be achieved with the
RouterConfig.
There's a comprehensive guide on configuration in the Configuration page.
3.3 Creating a Controller
A controller can be defined by simply giving a class the attribute of RouteController.
<?php declare(strict_types=1); namespace Example; use willitscale\Streetlamp\Attributes\Controller\RouteController; #[RouteController] class MyRouteClass { }
Only classes with the RouteController attribute will be scanned for routes.
3.4. Creating a Route
Each public method within a RouteController can be annotated as a route.
There's three requirements to transform a method into a route:
- add a HTTP method attribute to the method,
- a path attribute to the method or class and
- return the
ResponseBuilderobject.
Let's say we want to create a route for the request GET /hello HTTP/1.1, we would need to attribute our route method
with the Get and Path attributes.
Here's what that would look like in code:
<?php declare(strict_types=1); namespace Example; use Psr\Http\Message\ResponseInterface;use willitscale\Streetlamp\Attributes\Controller\RouteController;use willitscale\Streetlamp\Attributes\Path;use willitscale\Streetlamp\Attributes\Route\Method;use willitscale\Streetlamp\Builders\ResponseBuilder;use willitscale\Streetlamp\Enums\HttpMethod;use willitscale\Streetlamp\Enums\HttpStatusCode; #[RouteController] class MyRouteClass { #[Path('/hello')] #[Method(HttpMethod::GET)] public function simpleGet(): ResponseInterface { return new ResponseBuilder() ->setData('world') ->setHttpStatusCode(HttpStatusCode::HTTP_OK) ->build(); } }
You could also apply the #[Path('/hello')] attribute to the RouteController class itself. In this case, all routes
defined within the controller will have that path prefixed automatically, so you do not need to apply a path to each
method individually.
4. PSR Compliance
| PSR Standard | Description | Streetlamp Compliance |
|---|---|---|
| PSR-1 | Basic Coding Standard | ✅ |
| PSR-3 | Logger Interface | ✅ |
| PSR-4 | Autoloading Standard | ✅ |
| PSR-6 | Caching Interface | ❌ |
| PSR-7 | HTTP Message Interface | ✅ |
| PSR-11 | Container Interface | ❌ |
| PSR-12 | Extended Coding Style Guide | ✅ |
| PSR-13 | Hypermedia Links | ❌ |
| PSR-14 | Event Dispatcher | ❌ |
| PSR-15 | HTTP Handlers | ✅ |
| PSR-16 | Simple Caching | ✅ |
| PSR-17 | HTTP Factories | ❌ |
| PSR-18 | HTTP Client | ❌ |
5. Further Reading
willitscale/streetlamp 适用场景与选型建议
willitscale/streetlamp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 227 次下载、GitHub Stars 达 7, 最近一次更新时间为 2023 年 01 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 willitscale/streetlamp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 willitscale/streetlamp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 227
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-28