palabs / endpoint-bundle
Symfony Endpoint 组件
2.0.0
2021-12-24 04:59 UTC
Requires
- php: ^8.0
- symfony/config: ^5.0 || ^6.0
- symfony/dependency-injection: ^5.0 || ^6.0
- symfony/filesystem: ^5.0 || ^6.0
- symfony/http-foundation: ^5.0 || ^6.0
- symfony/http-kernel: ^5.0 || ^6.0
- symfony/routing: ^5.0 || ^6.0
README
PaEndpointBundle 为 symfony 控制器添加替代方案。Endpoint 是一个只包含一个方法 - execute(Request): Response 的控制器。
功能包括
- 简单且干净的 endpoint 接口 - 一个类中只有一个请求处理器
- 轻松生成路由,例如 $router->url(SomeEndpoint::class)
- 可重构 - 如果移动或重命名 endpoint,无需额外更改
- 无需路由名称(但如果你想,可以在路由定义中指定它)
- 支持扩展 endpoints - 你可以定义具有共享路由配置的基本 endpoints 和许多子 endpoints。这是 crud 控制器中非常常见的任务。
安装
将组件添加到你的 composer.json 文件中
composer require palabs/endpoint-bundle
在 Kernel 中注册组件
// app/AppKernel.php public function registerBundles() { return [ // ... new PaLabs\EndpointBundle\PaEndpointBundle(), // ... ]; }
在 app/config/routing.yml 中添加路由加载
endpoints: resource: . type: endpoints
用法
一个简单的 endpoint 看起来像
use PaLabs\EndpointBundle\EndpointInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class TextEndpoint implements EndpointInterface { public function routes() { return new Route('/cool_message'); } public function execute(Request $request): Response { return new Response('Hello, world'); } }