Skip to content
Back to skills

Ux Router

ASecurity

Symfony UX Router: generate Symfony route URLs in JavaScript, like path() and url() in Twig, type-checked in TypeScript. Use for createRouter, exposing routes (ux_router.routes, expose option), var/routes, ux:router:warm-cache.

  • 191 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 2, 2026
ai-agentsjavascripttypescriptgojavaphpbashexpressapidocumentation

Works with

  • api

Security analysis

A92/100
  • mediumInstalls packages at runtime which could introduce malicious dependencies

Pro shows the line behind each finding and how to fix it

Scanned December 2, 2026

npx -y skills add smnandre/symfony-ux-skills --skill ux-router --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Ux Router?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Ux Router
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/smnandre-ux-router/badge)](https://www.skillsdirectory.com/skills/smnandre-ux-router)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: ux-router
description: 'Symfony UX Router: generate Symfony route URLs in JavaScript, like path() and url() in Twig, type-checked in TypeScript. Use for createRouter, exposing routes (ux_router.routes, expose option), var/routes, ux:router:warm-cache.'
license: MIT
metadata:
  author: Simon André
  url: https://smnandre.dev
---

# UX Router

Generate the URLs of your Symfony routes in JavaScript, with the same result as the `path()` and `url()` Twig functions. Exposed routes are dumped to `var/routes/`; with TypeScript, route names and parameters are type-checked.

Since Symfony UX 3.6+. The package is **experimental**: its API may still change.

Prefer passing URLs from Twig (a Stimulus value, a `data-*` attribute) when a controller needs one or two fixed URLs. Reach for UX Router when JavaScript builds URLs from data it only knows at runtime.

## Installation

```bash
composer require symfony/ux-router
```

Flex creates the router module:

```javascript
// assets/router.js (rename to router.ts for type-checking)
import { createRouter } from '@symfony/ux-router';
import { routes, defaultLocale } from '../var/routes/index.js';

export const { path, url } = createRouter({ routes, defaultLocale });
```

| Asset pipeline | Extra step |
|----------------|------------|
| AssetMapper | None: the bundle registers the dump directory and the `@symfony/ux-router` importmap entry |
| Reprise (Vite / Rsbuild) | `npm install --force`, then restart the dev server |
| Webpack Encore | `npm install --force`, then restart Encore |

## Exposing Routes

No route is exposed by default. List name patterns:

```yaml
# config/packages/ux_router.yaml
ux_router:
    routes:
        - 'app_*'          # wildcard
        - '!app_admin_*'   # leading ! excludes
```

A single route can opt in or out with the `expose` option, which always wins over the patterns:

```php
#[Route('/search', name: 'search', options: ['expose' => true])]
public function search(): Response
```

A localized route is exposed when its name (`app_about`) or one of its variants (`app_about.en`) matches.

Exposed routes are readable by anyone who loads your JavaScript, and they all end up in the bundle. Keep the list to what JavaScript calls; avoid `'*'`, which also exposes internal routes such as the profiler's.

## Generating URLs

```javascript
import { path, url } from './router.js';

path('app_blog_show', { slug: 'hello', _query: { page: 2 }, _fragment: 'comments' });
// '/blog/hello?page=2#comments'

path('app_blog_show', { slug: 'hello' }, true);
// relative path: 'blog/hello'

url('app_blog_show', { slug: 'hello' });
// 'https://example.com/blog/hello'

url('app_blog_show', { slug: 'hello' }, true);
// network path: '//example.com/blog/hello'
```

Query parameters go in `_query`. Optional parameters equal to their default are omitted, as in PHP.

## Localized Routes

Call a localized route by its base name; the current locale picks the variant:

```javascript
path('app_about');                    // '/about' when <html lang="en">
path('app_about', { _locale: 'fr' }); // '/a-propos'
```

Locale resolution order: the `_locale` parameter of the call, the `_locale` of the `context` given to `createRouter()`, the `lang` attribute of `<html>` (`fr-FR` becomes `fr_FR`), the `defaultLocale` option (the dump exports `framework.default_locale`), then `en`.

## Request Context

Host, scheme and port come from the current page. Override them with `context`, for instance for an app served from a sub-directory:

```javascript
export const { path, url } = createRouter({
    routes,
    context: { baseUrl: '/my-app' },
});
```

## Errors

`RouteNotFoundError`, `MissingMandatoryParametersError` and `InvalidParameterError` are exported by `@symfony/ux-router` and thrown in the same cases as in PHP:

```javascript
import { RouteNotFoundError } from '@symfony/ux-router';

try {
    path('app_unknown');
} catch (error) {
    if (error instanceof RouteNotFoundError) {
        // ...
    }
}
```

Requirements are checked as JavaScript regexes (`[^/]+` instead of `[^/]++`); a requirement JavaScript cannot express, such as `\A` or `[[:alpha:]]`, is left to the PHP router.

## Dumping

Routes are dumped on cache warmup, and again in `dev` on the next request after a route changes. To refresh only the JavaScript routes:

```bash
php bin/console ux:router:warm-cache
```

## Configuration

```yaml
# config/packages/ux_router.yaml
ux_router:
    # Where routes and TypeScript types are dumped (update assets/router.js if changed)
    dump_directory: '%kernel.project_dir%/var/routes'

    # Dump TypeScript types next to the routes
    dump_typescript: true

    # Route name patterns to expose, "!" to exclude
    routes: []
```

AssetMapper ignores the TypeScript types, so they can be skipped in production there:

```yaml
when@prod:
    ux_router:
        dump_typescript: false
```

Keep them with Reprise or Encore and TypeScript: the build type-checks against them.

## Gotchas

- **`RouteNotFoundError` for an existing route**: the route is hidden from JavaScript. Check the `routes` patterns (an exclusion may match) or add `options: ['expose' => true]`.
- **Stale routes after a change in production**: the dump happens on cache warmup; deploy with `cache:warmup` or run `ux:router:warm-cache`.
- **Wrong host or base path behind a proxy or in a sub-directory**: pass `context` to `createRouter()`.
- **TypeScript errors on route names**: the types come from the dump; run `ux:router:warm-cache` after adding a route.

## References

- [Symfony UX package page](https://ux.symfony.com/router) ([Markdown summary](https://ux.symfony.com/router.md)) · [`symfony/ux-router` on Packagist](https://packagist.org/packages/symfony/ux-router).
- Try the [JavaScript routing demo](https://ux.symfony.com/router#demo).
- For route exposure, URL generation and TypeScript, see the [Symfony UX Router documentation](https://symfony.com/bundles/ux-router).

## See Also

- **stimulus**: import `path()` in a controller to build URLs from targets or values
- **turbo**: `Turbo.visit(path('app_blog_show', { slug }))` navigates with Drive
- **ux-translator**: same dump model for translations (`var/translations/`, `trans()` in JavaScript)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.