palabs/endpoint-bundle

Symfony Endpoint 组件

安装次数: 3,033

依赖者: 0

建议者: 0

安全性: 0

星标: 4

关注者: 2

分支: 0

开放问题: 1

类型:symfony-bundle

2.0.0 2021-12-24 04:59 UTC

This package is auto-updated.

Last update: 2024-09-29 22:30:14 UTC


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');
    }
}