binsoul/symfony-bundle-routing
Installation:
composer require binsoul/symfony-bundle-routing
Register the bundle in config/bundles.php (Symfony) or config/app.php (Laravel via Symfony bridge):
return [
// ...
Binsoul\RoutingBundle\RoutingBundle::class => ['all' => true],
];
First Use Case:
Dynamically generate routes or validate existing ones without manual YAML/XML annotations.
Example (Symfony-style, adaptable to Laravel via Route facade):
use Binsoul\RoutingBundle\Routing\Router;
$router = $this->container->get('binsoul_routing.router');
$route = $router->generate('app_homepage', ['_locale' => 'en']);
Where to Look First:
src/Routing/Router.php for core logic.config/packages/binsoul_routing.yaml for default configurations.tests/ for usage examples and edge cases.Dynamic Route Generation:
Use the Router service to generate URLs at runtime with parameters:
$url = $router->generate('user_profile', ['id' => 123, '_locale' => 'fr']);
Route Validation: Validate if a route exists before generation:
if ($router->hasRoute('app_homepage')) {
$url = $router->generate('app_homepage');
}
Laravel Integration:
Bind the Symfony router to Laravel’s Route facade via a service provider:
public function register()
{
$this->app->singleton('binsoul_routing.router', function ($app) {
return $app->make('router')->getRouter();
});
}
Custom Route Loaders:
Extend Binsoul\RoutingBundle\Loader\RouteLoaderInterface to load routes from custom sources (e.g., database):
class DatabaseRouteLoader implements RouteLoaderInterface
{
public function load(RouteCollection $collection)
{
$routes = DB::table('routes')->get();
foreach ($routes as $route) {
$collection->add($route->name, new Route($route->path, [...]));
}
}
}
Middleware Integration: Attach middleware to routes dynamically:
$route = new Route('/admin', [...]);
$route->addRequirement('_role', 'admin');
$collection->add('admin_dashboard', $route);
Symfony\Bridge\Laravel\Routing\LaravelRouter to share route collections between frameworks.binsoul_routing.yaml:
binsoul_routing:
cache: true
$router->generate('homepage', ['_locale' => 'es']);
Namespace Collisions:
Ensure route names are unique across bundles/applications. Prefix with bundle names (e.g., binsoul_homepage).
Laravel-Specific Quirks:
Router interface. For Laravel, wrap it in a facade or service:
Route::generate('route_name'); // Custom facade method
Route helpers with Symfony’s Router methods (e.g., route('name') vs $router->generate()).Configuration Overrides:
Default configs in binsoul_routing.yaml may conflict with Laravel’s routes.php. Merge them explicitly:
$this->mergeConfigFrom(__DIR__.'/binsoul_routing.php', 'binsoul_routing');
Route Collection Immutability:
The RouteCollection is immutable after loading. Create a new instance or use add() carefully:
$collection = new RouteCollection();
$collection->add('dynamic_route', new Route('/dynamic', [...]));
dump($router->getRouteCollection()->all());
_locale parameters fail, check binsoul_routing.yaml for default locale settings:
binsoul_routing:
default_locale: en
Custom Route Attributes:
Extend Binsoul\RoutingBundle\Annotation\Route to add metadata:
#[Route(name: 'custom_route', path: '/custom', attributes: ['priority' => 100])]
Event Listeners:
Subscribe to binsoul_routing.route_loaded events to modify routes post-load:
$dispatcher->addListener('binsoul_routing.route_loaded', function (RouteLoadedEvent $event) {
$event->getRoute()->setDefault('_priority', 50);
});
Route Compiler:
Override Binsoul\RoutingBundle\Compiler\RouteCompiler to customize route matching logic (e.g., regex patterns).
Testing:
Mock the Router service in PHPUnit:
$router = $this->createMock(Router::class);
$router->method('generate')->willReturn('/mocked-url');
$this->app->instance('binsoul_routing.router', $router);
How can I help you explore Laravel packages today?