Skip to main content
The route() function is Ziggy’s primary helper for generating URLs from Laravel route names in JavaScript. It can return either a URL string or a Router instance depending on how it’s called.

Function Signature

Parameters

string
The name of the route to generate a URL for. If omitted or undefined, returns a Router instance instead of a URL string.Example: 'posts.show', 'users.edit'
string | number | array | object
Route parameters to fill in the route’s URI template. Can be:
  • A single value (string/number) for routes with one parameter
  • An array of values in parameter order
  • An object with parameter names as keys
  • Model objects with id property or custom route key bindings
Example: 1, [1, 2], { post: 1, comment: 5 }, { post: { id: 1 } }
boolean
default:"true"
Whether to generate an absolute URL (including the origin) or a relative path.
  • true: Returns full URL like https://example.com/posts/1
  • false: Returns relative path like /posts/1
Config
Optional Ziggy configuration object. If not provided, uses the global Ziggy variable or attempts to load config from a #ziggy-routes-json script tag.Properties:
  • url: Application URL
  • port: Application port (or null)
  • defaults: Default parameter values
  • routes: Route definitions object
  • location: Override current location (for SSR)

Return Value

  • When called with a route name: Returns a string containing the generated URL
  • When called without arguments or with name as undefined: Returns a Router instance for advanced usage

Examples

Basic Usage

Object Parameters

Query Parameters

Relative URLs

Custom Configuration

Getting a Router Instance

Error Handling

The route() function throws errors in the following cases:

TypeScript Support

Ziggy provides full TypeScript support with autocomplete for route names and type-checked parameters:
Generate TypeScript definitions with:

Implementation Details

The route() function is a lightweight wrapper around the Router class:
When called with a route name, it:
  1. Creates a new Router instance
  2. Parses and normalizes the parameters
  3. Compiles the route template with the parameters
  4. Returns the generated URL string
When called without a name, it returns the Router instance directly for use with methods like current(), has(), and accessing the params property.

See Also