fys / middleware-bundle
provide simple middleware configuration for symfony 4
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=7.1.0
This package is auto-updated.
Last update: 2025-01-16 04:44:02 UTC
README
Provides symfony 4 simple middleware by forwarding event kernel.request
and kernel.response
to your middleware
Getting Started
Installation
composer require fys/middleware-bundle
Creating your first middleware
- Create service definition for your middleware class and tag
middleware
on your service definition (in this case we will createAuthMiddleware
inApp\Middleware\AuthMiddleware
)
// app/config/services.yaml
services:
App\Middleware\AuthMiddleware:
tags: ["middleware"]
// App\Middleware\AuthMiddleware
<?php
namespace App\Middleware\AuthMiddleware;
use FYS\MiddlewareBundle\Contract\MiddlewareInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
class AuthMiddleware implements MiddlewareInterface
{
public function getAlias()
{
return 'auth';
}
public function onRequest(GetResponseEvent $event)
{
// do your action here
}
public function onResponse(FilterResponseEvent $event)
{
// do your action here
}
}
- apply your middleware in controller by implements
FYS\MiddlewareBundle\Contract\MiddlewareControllerInterface
, by implementing this interface, your controller should haverunMiddleware()
method with returnstring
// App\Controller\TestController
<?php
namespace App\Controller\TestController;
use FYS\MiddlewareBundle\Contract\MiddlewareControllerInterface;
class TestController implements MiddlewareControllerInterface
{
// your logic controller
public function runMiddleware()
{
return 'auth';
}
}
- Done! now every request to TestController will apply AuthMiddleware