Skip to main content
The Router class provides Ziggy’s advanced routing features, including checking the current route, inspecting parameters, and verifying route existence. When you call route() without arguments, it returns a Router instance.

Class Definition

The Router class extends String, so Router instances can be used anywhere a string is expected and will automatically convert to their URL representation.

Constructor Parameters

string
Optional route name. If provided, the Router will be configured for that specific route.
string | number | array | object
Optional route parameters. Can be a single value, array, or object with parameter names as keys.
boolean
default:"true"
Whether to generate absolute URLs (including the origin) or relative paths.
Config
Optional Ziggy configuration. Falls back to the global Ziggy variable or #ziggy-routes-json script tag.

Methods

current()

Check the current route name or test if the current URL matches a specific route.

Signature

Parameters

string
Optional route name to check against the current URL. Supports wildcards using *.Example: 'posts.*' matches 'posts.index', 'posts.show', etc.
string | number | array | object
Optional route parameters to verify against the current URL parameters.

Return Value

  • Without arguments: Returns the name of the current route as a string, or undefined if no match
  • With arguments: Returns boolean indicating whether the current route matches

Examples

has()

Check whether a route with the given name exists in the route list.

Signature

Parameters

string
required
The route name to check for existence.

Return Value

Returns boolean - true if the route exists, false otherwise.

Examples

toString()

Generate and return the compiled URL string for the route. This method is called automatically when the Router is used in a string context.

Signature

Return Value

Returns the compiled URL as a string.

Examples

Properties

params

Get all parameters (both route parameters and query parameters) from the current URL.

Type

Return Value

Returns an object containing all route parameters and query string parameters from the current URL.

Examples

routeParams

Get only the route parameters (excluding query string parameters) from the current URL.

Type

Examples

queryParams

Get only the query string parameters from the current URL.

Type

Examples

Usage Patterns

Getting a Router Instance

Conditional Navigation

Route-Aware Components

Parameter Inspection

Direct Instantiation

You can also import and instantiate the Router class directly:

Configuration

The Router automatically loads configuration from:
  1. The config parameter (if provided)
  2. A global Ziggy variable
  3. A globalThis.Ziggy variable
  4. A <script id="ziggy-routes-json"> tag in the document

Error Handling

The Router throws errors for invalid routes:

TypeScript Support

The Router class is fully typed:

See Also