Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/tighten/ziggy/llms.txt

Use this file to discover all available pages before exploring further.

Overview

Ziggy’s route() function works like Laravel’s route() helper—you can pass it the name of a route, and the parameters you want to pass to the route, and it will generate a URL.

Function Signature

The route() function accepts up to four parameters:
route(
  name?: string,
  params?: string | number | array | object,
  absolute?: boolean,
  config?: object
): string | Router

Parameters

name
string
The name of the Laravel route you want to generate a URL for.
params
string | number | array | object
Route parameters to substitute into the route’s URI template. Can be passed as:
  • A single value (string or number)
  • An array of values
  • An object with parameter names as keys
absolute
boolean
default:"true"
Whether to include the URL origin (domain) in the generated URL. Set to false to generate a relative path.
config
object
Ziggy configuration object. When using the @routes Blade directive, this is available globally and doesn’t need to be passed.

Return Value

The route() function returns:
  • String: A fully-formed URL when called with a route name
  • Router instance: A Router class instance when called with no arguments, providing access to additional methods like current(), has(), and the params property

Basic Usage

Generate a simple URL:
route('posts.index');
// 'https://ziggy.test/posts'
Generate a relative URL:
route('posts.index', [], false);
// '/posts'

With Route Parameters

route('posts.show', 1);
// 'https://ziggy.test/posts/1'

Working with the Router Class

When called without arguments, route() returns a Router instance:
const router = route();

// Check the current route
router.current(); // 'posts.show'

// Check if a route exists
router.has('posts.index'); // true

// Get current route parameters
router.params; // { post: '1', page: '5' }

Practical Examples

HTTP Request with Axios

const post = { id: 1, title: 'Ziggy Stardust' };

return axios.get(route('posts.show', post)).then((response) => response.data);
<a href="{route('posts.index')}">View all posts</a>

Form Actions

<form method="POST" action="{route('posts.store')}">
  <!-- form fields -->
</form>

Error Handling

If you pass a route name that doesn’t exist in your route list, Ziggy will throw an error:
route('nonexistent.route');
// Error: Ziggy error: route 'nonexistent.route' is not in the route list.
When using the @routes Blade directive, Ziggy’s configuration is available globally. If you’re importing Ziggy manually (e.g., in a SPA), you’ll need to pass the configuration as the fourth parameter.

Next Steps

Route Parameters

Learn how to pass parameters to routes

Query Parameters

Add query strings to your URLs