bengor-user/symfony-routing-bridge
Adapter bridge that integrates BenGorUser’s User library with Symfony Routing, enabling user-related routing compatibility. Install via Composer and run the fully PHPSpec-tested suite locally.
Installation Add the package via Composer:
composer require bengor-user/symfony-routing-bridge
Register the service provider in config/app.php:
'providers' => [
// ...
BenGorUser\SymfonyRoutingBridge\SymfonyRoutingBridgeServiceProvider::class,
],
Basic Usage Publish the config file (if needed) and bind Laravel routes to Symfony’s router:
use BenGorUser\SymfonyRoutingBridge\Facades\SymfonyRouter;
// In a controller or route closure
$route = SymfonyRouter::getRouteCollection();
$matcher = SymfonyRouter::getMatcher();
$generator = SymfonyRouter::getGenerator();
First Use Case Convert a Laravel route to a Symfony-compatible URL:
$url = SymfonyRouter::generate('route.name', ['param' => 'value']);
Route Matching Use Symfony’s matcher to analyze incoming requests:
$request = app('request');
$context = SymfonyRouter::getContext();
$matchResult = SymfonyRouter::getMatcher()->match($request->path(), $context);
URL Generation Generate absolute/relative URLs from Laravel routes:
// Absolute URL
$absoluteUrl = SymfonyRouter::generate('route.name', [], true);
// Relative URL
$relativeUrl = SymfonyRouter::generate('route.name');
Route Collection Integration Merge Laravel’s route collection with Symfony’s:
$symfonyRoutes = SymfonyRouter::getRouteCollection();
$laravelRoutes = app('router')->getRoutes();
Middleware Integration Leverage Symfony’s route attributes for Laravel middleware:
// Define middleware in Symfony route attributes
$route->setDefault('_middleware', ['auth', 'throttle']);
RouteCollection for complex logic while keeping Laravel’s simplicity for basic routes.SymfonyRouter in unit tests:
$this->app->instance('symfony.router', MockSymfonyRouter::class);
Route Naming Conflicts
Ensure Laravel route names (e.g., home.index) don’t clash with Symfony’s reserved names (e.g., _wildcard).
Context Mismatches
Symfony’s Context expects specific parameters (e.g., host, scheme). Override defaults:
$context = SymfonyRouter::getContext();
$context->setHost('api.example.com');
Deprecated Methods
The package is outdated (2017). Avoid relying on undocumented methods like SymfonyRouter::getRouter().
CSRF Token Issues
Symfony’s UrlGenerator may not handle Laravel’s CSRF tokens. Manually add them:
$url = SymfonyRouter::generate('route.name') . '?_token=' . csrf_token();
RouteDumper to inspect routes:
$dumper = new \Symfony\Component\Routing\RouteDumper\HtmlDumper();
$dumper->dump($symfonyRoutes);
try {
$matcher->match('/path');
} catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
\Log::error('Route not found: ' . $e->getMessage());
}
Custom Route Loaders
Extend SymfonyRouter to load routes from external sources (e.g., database):
SymfonyRouter::addRouteCollection($customRoutes);
Override Generators
Replace the default UrlGenerator with a custom implementation:
$generator = new CustomUrlGenerator($symfonyRoutes, $context);
SymfonyRouter::setGenerator($generator);
Event Listeners
Hook into Symfony’s ROUTE_MATCH and ROUTE_GENERATE events via Laravel’s event system:
event(new SymfonyRouteMatched($matchResult));
How can I help you explore Laravel packages today?