承接 pierotto/symfony-middleware-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pierotto/symfony-middleware-bundle

最新稳定版本:1.1.0

Composer 安装命令:

composer require pierotto/symfony-middleware-bundle

包简介

This Symfony extension allows for the integration of middleware into your application, providing a streamlined approach to handling HTTP requests and responses.

README 文档

README

This Symfony extension makes it possible to implement middleware for editing requests and responses within a Symfony application. Middlewares are registered as services in a container and run in the order they are defined in code.

Installation

The package can be installed using Composer with the command::

$ composer require pierotto/symfony-middleware-bundle

After installing the package, it needs to be registered in AppKernel.php:

public function registerBundles(): array
{
    $bundles = [
        new \Pierotto\MiddlewareBundle\MiddlewareBundle(),
    ];
}

Usage

To use middleware, you need to create your own class that implements \Psr\Http\Server\MiddlewareInterface. Then this class can be registered as a service in the container and set the middleware tag.

For example, request modification middleware can be implemented as follows:

namespace Api\Http\Middleware;

class CustomMiddleware implements \Psr\Http\Server\MiddlewareInterface
{

	public function process(
		\Psr\Http\Message\ServerRequestInterface $request,
		\Psr\Http\Server\RequestHandlerInterface $handler
	): \Psr\Http\Message\ResponseInterface
	{
		return $handler->handle($request->withAttribute('test', 'test'));
	}

}

The response editing middleware could look like this:

namespace Api\Http\Middleware;

class NotFoundMiddleware implements \Psr\Http\Server\MiddlewareInterface
{

	public function process(
		\Psr\Http\Message\ServerRequestInterface $request,
		\Psr\Http\Server\RequestHandlerInterface $handler
	): \Psr\Http\Message\ResponseInterface
	{
		return new \Nyholm\Psr7\Response\Response(404);
	}

}

Then you need to register the created middleware as services in services.yml and set the middleware tag to them:

Api\Http\Middleware\CustomMiddleware:
        tags: [ 'middleware' ]

Middleware can then be used when calling controller methods by adding the \Application\MiddlewareBundle\Infrastructure\Attribute\Middleware attribute with the value of the middleware class name. The middleware is started in the order in which the attributes are defined.

#[\Application\MiddlewareBundle\Infrastructure\Attribute\Middleware(Api\Http\Middleware\CustomMiddleware::class)]
	public function defaultAction(
		\Symfony\Component\HttpFoundation\Request $request
	): \Symfony\Component\HttpFoundation\Response

统计信息

  • 总下载量: 16
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-28