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

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

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

reindeer/symfony-middleware

最新稳定版本:v1.0.2

Composer 安装命令:

composer require reindeer/symfony-middleware

包简介

Middleware support for symfony

README 文档

README

Introduction

This bundle provides PSR-15-like middlewares in Symfony. Unlike PSR-15 it uses common Symfony Requests and Responses.

Installation

composer require reindeer/symfony-middleware

Usage

Each middleware must implement the Reindeer\SymfonyMiddleware\Contracts\MiddlewareInterface. This interface is very similar to Psr\Http\Server\MiddlewareInterface but uses Symfony\Component\HttpFoundation\Request and Symfony\Component\HttpFoundation\Response instead of Psr\Http\Message\RequestInterface and Psr\Http\Message\ResponseInterface respectively.

Some middleware for a route can be applied using options array in route description.

Protect route with a middleware

Let's create a middleware which authentication checks:

src/Middleware/AuthMiddleware.php

<?php

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Reindeer\SymfonyMiddleware\Contracts\MiddlewareInterface;
use Reindeer\SymfonyMiddleware\Contracts\RequestHandlerInterface;

class CheckDeposit implements MiddlewareInterface
{
    public function process(Request $request, RequestHandlerInterface $handler): Response
    {
        // Check credentials
        
        return $handler->handle($request);
    }
}

Now we need to protect the route using our middleware:

config/routes.yaml

custom.route:
  path: /some/path
  options:
    middleware:
      - \App\Middleware\AuthMiddleware

That's all.

You can add as much middlewares for every route as you need.

Please note: if you use import routes and set middlewares both in routes and collection only collection middlewares are used.

config/imported-routes.yaml

custom.route:
  path: /some/path
  options:
    middleware:
      - \App\Middleware\YetAnotherMiddleware # this middleware will be ignored

config/routes.yaml

collection:
  resource: 'imported-routes.yaml'
  options:
    middleware:
      - \App\Middleware\CustomMiddleware

Examples

Before middleware to check authentication

<?php

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Reindeer\SymfonyMiddleware\Contracts\MiddlewareInterface;
use Reindeer\SymfonyMiddleware\Contracts\RequestHandlerInterface;

class CheckDeposit implements MiddlewareInterface
{
    public function process(Request $request, RequestHandlerInterface $handler): Response
    {
        if (!isAuthenticated($request->headers->get('Authorization')) {
            throw new UnauthorizedHttpException();
        }
        
        return $handler->handle($request);
    }
    
    protected function isAuthenticated($token): bool
    {
        return true; // Do some stuff
    }
}

After middleware to send logs to external service

<?php

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Reindeer\SymfonyMiddleware\Contracts\MiddlewareInterface;
use Reindeer\SymfonyMiddleware\Contracts\RequestHandlerInterface;

class CheckDeposit implements MiddlewareInterface
{
    public function process(Request $request, RequestHandlerInterface $handler): Response
    {   
        $response = $handler->handle($request);
        
        // Send logs to kibana
        
        return $response
    }
}

License

The MIT License (MIT).

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-16