indragunawan / middleware-bundle
Before and After filter implementation by using annotation
Installs: 26
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 1
Type:symfony-bundle
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-12-29 06:02:30 UTC
README
Middleware bundle provide simple implementation of symfony before and after filter by using annotation. this implementation is inspired by Laravel Middleware.
Installation
If your project already uses Symfony Flex, execute this command to download, register and configure the bundle automatically:
composer require indragunawan/middleware-bundle
If you install without using Symfony Flex, first add the bundle by using composer then enable the bundle by adding new Indragunawan\MiddlewareBundle\IndragunawanMiddlewareBundle()
to the list of registered bundles in the app/AppKernel.php file of your project.
Create middleware service
<?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 } }
Usage
You only need to create the annotation on class level.
<?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 }
TODO
- Add tests
License
This bundle is under the MIT license. See the complete license