Before and After filter implementation by using annotation

dev-master 2018-11-30 20:22 UTC

This package is auto-updated.

Last update: 2022-05-29 01:46:07 UTC


license Travis Scrutinizer Coverage Scrutinizer Source Packagist

Middleware bundle provide simple implementation of symfony before and after filter by using annotation. this implementation is inspired by Laravel Middleware.


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

// 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


You only need to create the annotation on class level.

// 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


  • Add tests


This bundle is under the MIT license. See the complete license