a5sys/logged-in-redirection-bundle
Install the Package
composer require a5sys/logged-in-redirection-bundle
Register the Bundle
Add to config/bundles.php (Laravel 5.4+) or AppKernel.php (Symfony):
A5sys\LoggedInRedirectionBundle\LoggedInRedirectionBundle::class => ['all' => true],
Configure Routes
Add to config/packages/logged_in_redirection.yaml (or config.yml in Symfony):
logged_in_redirection:
route_name: "fos_user_security_login" # Route to check for logged-in users
redirect_route_name: "homepage" # Route to redirect to
First Use Case
A user bookmarks the login page (/login). After logging in, they return to the bookmark and see the login form again. The bundle automatically redirects them to homepage (e.g., /).
Trigger Logic
The bundle hooks into Symfony’s kernel.request event. When a user visits the configured route_name (e.g., /login) after authentication, the bundle checks if they’re logged in. If they are, it redirects to redirect_route_name.
Integration with Authentication
symfony/http-foundation) to integrate with Laravel’s auth middleware.Dynamic Redirects
Override the redirect_route_name per request via a twig variable or controller logic:
# config/packages/logged_in_redirection.yaml
logged_in_redirection:
route_name: "fos_user_security_login"
redirect_route_name: "dashboard" # Default
Or dynamically in a controller:
$this->get('logged_in_redirection.redirector')->setRedirectRoute('custom_dashboard');
Excluding Routes Disable the redirect for specific routes by tagging them in routing:
# config/routes.yaml
login:
path: /login
controller: App\Controller\LoginController::index
methods: GET
defaults:
_logged_in_redirection_exclude: true
Laravel-Specific Adaptation
// app/Http/Middleware/LoggedInRedirect.php
public function handle($request, Closure $next) {
if (Auth::check() && $request->is('login')) {
return redirect()->route('homepage');
}
return $next($request);
}
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->bind(
'logged_in_redirection.redirector',
A5sys\LoggedInRedirectionBundle\Redirector::class
);
}
Route Name Mismatch
route_name doesn’t exist.config/packages/logged_in_redirection.yaml matches your routing (e.g., fos_user_security_login for FOSUserBundle).Caching Headaches
redirect_route_name is a non-cacheable route (e.g., add stale-while-revalidate: 0 to cache headers).Laravel Auth Guard Conflicts
Security component. In Laravel, ensure the guard is properly bound:
Auth::guard('web')->check(); // Explicit guard check
CSRF Token Errors
Enable Debug Mode
Add this to config/packages/logged_in_redirection.yaml:
logged_in_redirection:
debug: true # Logs redirects to Symfony's profiler
Check Events
The bundle listens to kernel.request. Debug with:
// In a controller or command
$eventDispatcher = $this->get('event_dispatcher');
$eventDispatcher->addListener('kernel.request', function($event) {
dump($event->getRequest()->getPathInfo());
});
Override Redirector
Extend the Redirector class to add custom logic:
// src/Service/CustomRedirector.php
namespace App\Service;
use A5sys\LoggedInRedirectionBundle\Redirector as BaseRedirector;
class CustomRedirector extends BaseRedirector {
public function isRedirectable($request) {
if ($request->getPathInfo() === '/admin') {
return false; // Skip redirect for /admin
}
return parent::isRedirectable($request);
}
}
Bind it in config/services.yaml:
services:
logged_in_redirection.redirector: '@App\Service\CustomRedirector'
Custom Redirect Logic
Subscribe to the logged_in_redirection.redirect event:
// config/services.yaml
services:
App\EventListener\CustomRedirectListener:
tags:
- { name: kernel.event_listener, event: logged_in_redirection.redirect, method: onRedirect }
// src/EventListener/CustomRedirectListener.php
public function onRedirect(RedirectEvent $event) {
if ($event->getUser()->hasRole('ADMIN')) {
$event->setRedirectRoute('admin_dashboard');
}
}
Multi-Tenant Redirects
Dynamically set redirect_route_name based on tenant:
$redirector = $this->get('logged_in_redirection.redirector');
$redirector->setRedirectRoute("tenant_{$tenantId}_dashboard");
AJAX Handling Exclude AJAX requests from redirects:
logged_in_redirection:
exclude_ajax: true
Or in custom logic:
if ($request->isXmlHttpRequest()) {
return $next($request);
}
How can I help you explore Laravel packages today?