indragunawan / middleware-bundle
使用注解实现前后端过滤器
dev-master
2018-11-30 20:22 UTC
Requires
- doctrine/common: ^2.4
- symfony/config: ^3.4|^4.0
- symfony/dependency-injection: ^3.4|^4.0
- symfony/http-foundation: ^3.4|^4.0
- symfony/http-kernel: ^3.4|^4.0
- webmozart/assert: ^1.3
Requires (Dev)
- phpunit/phpunit: ^7.4
This package is auto-updated.
Last update: 2024-08-29 05:06:20 UTC
README
Middleware bundle 通过使用注解提供了对 symfony 前后端过滤器 的简单实现。此实现受到了 Laravel Middleware 的启发。
安装
如果你的项目已经使用 Symfony Flex,执行以下命令以自动下载、注册和配置该组件:
composer require indragunawan/middleware-bundle
如果你不使用 Symfony Flex 安装,首先使用 composer 添加组件,然后将 new Indragunawan\MiddlewareBundle\IndragunawanMiddlewareBundle()
添加到项目 app/AppKernel.php 文件中已注册组件的列表中,以启用组件。
创建中间件服务
<?php // src/Middleware/Subscribed.php use Indragunawan\MiddlewareBundle\Middleware\BeforeFilterInterface; use Indragunawan\MiddlewareBundle\Middleware\AfterFilterInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class Subscribed implements BeforeFilterInterface //, AfterFilterInterface // you can implement multi filter at once { public static function supports() { return 'subscribed'; // return the filter name // return ['subscribed', 10] // return [filter_name, priority] same like usual Symfony event } // implement this method for BeforeFilter public function onBeforeFilter(Request $request, array $controller, ?int $requestType) { // your logic } // implement this method for AfterFilter public function onAfterFilter(Request $request, Response $response, array $controller, ?int $requestType) { // your logic } }
用法
你只需要在类级别创建注解。
<?php // app/Controller/HomepageController.php use Indragunawan\MiddlewareBundle\Annotation\BeforeFilter; use Indragunawan\MiddlewareBundle\Annotation\AfterFilter; /** * @BeforeFilter("subscribed") // the 'subscribed' middleware will execute before every actions at this controller. * @BeforeFilter("subscribed", only="index") // the 'subscribed' middleware will execute ONLY before 'index' action at this controller. * @BeforeFIlter("subscribed", except="create") // the 'subscribed' middleware will execute before every actions at this controller EXCEPT 'create' action. * * @AfterFilter({"subscribed", "other"}) // the 'subscribed' and 'other' middleware will execute after every actions at this controller. * @AfterFilter({"subscribed", "other"}, only={"index","delete"}) // the 'subscribed' and 'other' middleware will execute ONLY after 'index' and 'delete' action at this controller. * @AfterFilter({"subscribed", "other"}, except={"create","update"}) // the 'subscribed' and 'other' middleware will execute after every actions at this controller EXCEPT 'create' and 'update' action. * * BeforeFilter and AfterFilter receive same arguments format. */ class HomepageController extends AbstractController { // your action }
待办事项
- 添加测试
许可证
此组件受 MIT 许可证保护。请参阅完整的 许可证