This Neos Flow package provides a PSR-15 middleware to conditionally redirect requests based on Eel expressions. It is designed to intercept requests after routing and security entry points, but before dispatching to a controller.
- Eel-based conditions: Use powerful Eel expressions to decide when a redirect should happen.
- Role-based checks: Includes a
securityContextEel helper for role membership and authentication checks. - Global Whitelist: Prevent redirection for specific routes (e.g., login, logout, authentication) using a configurable whitelist.
- Redirect Loop Protection: Limits the number of consecutive redirects to prevent infinite loops, with configurable maximum redirects.
- Original Request Storage: Automatically stores the original request URI in the session, allowing targets to redirect the user back after handling the condition (e.g., after successful onboarding).
- Custom Status Codes: Support for different HTTP redirect status codes (default is 303).
Add the package via composer:
{
"require": {
"fucodo/conditional-redirect": "*"
}
}The package is configured via Settings.yaml.
fucodo:
conditional:
redirect:
enabled: true
# Rules are checked in order. The first matching rule wins.
rules:
onboardingRedirect:
condition: "${securityContext.isAuthenticated() && !securityContext.hasRole('Some.Package:AlreadyOnboarded')}"
target:
package: 'Some.Package'
controller: 'Onboarding'
action: 'index'
arguments: {}
statusCode: 303
# Whitelist targets that should NEVER be redirected
whitelist:
login:
enabled: true
package: 'Neos.Neos'
controller: 'Login'
action: 'index'
logout:
enabled: true
package: 'Neos.Neos'
controller: 'Login'
action: 'logout'
# Redirect loop protection
redirectLoop:
maxRedirects: 5
counterSessionKey: 'fucodo_conditional_redirect_Counter'
originalRequestSessionKey: 'fucodo_conditional_redirect_OriginalRequest'
# Whether to store the original request URI in the session
storeOriginalRequest: trueThe following variables are available in the Eel expressions:
request: The currentServerRequestInterface.actionRequest: The resolvedActionRequest.controllerObjectName: The name of the target controller class.controllerName: The short name of the target controller.actionName: The name of the target action.packageKey: The package key of the target.arguments: The arguments for the action.account: The currently authenticated account (if any).securityContext: An instance offucodo\conditional\redirect\Eel\SecurityHelper.
securityContext.hasRole(roleIdentifier): Returns true if the current account has the specified role.securityContext.isAuthenticated(): Returns true if an account is authenticated.securityContext.getAccountIdentifier(): Returns the identifier of the current account or null.
The middleware is automatically registered in Settings.Neos.Flow.http.yaml and positioned after securityEntryPoint to ensure authentication data is available for the Eel expressions.