FOSJsRoutingBundle
Expose Symfony's routing configuration to JavaScript so client code can generate URLs by route name
Repository Health
Technical Analysis
FOSJsRoutingBundle is a Symfony bundle that exposes the application’s routing configuration to JavaScript, mirroring the server-side Router component so client-side code can generate URLs using the same route names and parameters used in PHP controllers and Twig templates. Routes are tagged as exposed and served either through a controller endpoint or dumped to a static JS/JSON file at build time, then consumed by a small Router JS class bundled with the package.
This avoids hardcoding URL paths in JavaScript or duplicating routing logic between the server and client — when a route changes in Symfony’s routing configuration, exposed JS-side URLs stay in sync automatically as long as the exposed route dump is regenerated.
What You Get
- A DumpRoutesCommand console command to dump exposed routes to a static JS or JSON file
- An ExposedRoutesController serving routing data dynamically via an HTTP endpoint
- A JavaScript Router class (AMD/CommonJS/global builds) for generating URLs client-side
- Route exposure via the existing Symfony route configuration (options: expose: true)
- Webpack and TypeScript integration resources under Resources/webpack and Resources/ts
Common Use Cases
- Generating API/page URLs in a JavaScript single-page section of a Symfony app without hardcoding paths
- Keeping client-side and server-side URL generation in sync as routes change
- Building AJAX request URLs by route name and parameters instead of string concatenation
- Serving a locale-aware routing dump for multilingual Symfony applications
Under The Hood
Architecture - The bundle hooks into Symfony’s DependencyInjection container (DependencyInjection/) to register services around Symfony’s own RouterInterface, then exposes two consumption paths: Controller/ExposedRoutesController serves routing data as an HTTP response for apps that want it fetched dynamically, while Command/DumpRoutesCommand writes a static JS/JSON dump for apps that prefer a build-time asset. Extractor/ contains the logic that filters the full Symfony route collection down to only routes explicitly marked expose: true, and Serializer/Normalizer+Denormalizer convert that filtered route collection to/from the wire format the JS Router consumes. The bundled Resources/js/router.js (with AMD/CommonJS/UMD builds) reimplements Symfony’s route-to-URL generation algorithm in JavaScript against that served data. Tech Stack - PHP 8.0+ Symfony bundle depending on symfony/framework-bundle, symfony/console, and symfony/serializer; the JS side has no build dependency beyond the bundled UMD file, with optional Webpack/TypeScript resources for modern front-end toolchains. Code Quality - Tests/ covers the controller, command, extractor, and serializer/normalizer logic with PHPUnit (via symfony/phpunit-bridge); the codebase follows standard Symfony bundle conventions (services.yaml-style DI configuration, PSR-4 autoloading). API Design - Route exposure reuses Symfony’s existing route configuration mechanism (an expose option on route definitions) rather than introducing a parallel config format, and the JS Router’s generate(routeName, params) method intentionally mirrors the PHP-side UrlGeneratorInterface::generate() signature so developers moving between server and client code don’t have to learn a second API.