window.Ziggy = null; window.route = function(name, params, absolute, config = window.Ziggy) { const router = new (class { constructor(name, params, absolute, config) { this.name = name; this.params = params ?? {}; this.absolute = absolute ?? true; this.config = config; } toString() { const route = this.config.routes[this.name]; if (!route) { throw new Error(`Route [${this.name}] not found.`); } let uri = route.uri; const params = typeof this.params === 'object' ? this.params : [this.params]; // Extract parameters from URI if not provided by Ziggy let routeParams = route.parameters || []; if (routeParams.length === 0) { const matches = uri.match(/{([^}]+)\??}/g); if (matches) { routeParams = matches.map(m => m.replace(/[{}?]/g, '')); } } // Replace route parameters routeParams.forEach((param, index) => { const value = Array.isArray(params) ? params[index] : params[param]; if (value !== undefined) { uri = uri.replace(new RegExp(`{${param}\\??}`, 'g'), value); } }); // Remove optional parameters that weren't provided uri = uri.replace(/{[^}]+\?}/g, ''); if (this.absolute) { const baseUrl = this.config.url.replace(/\/+$/, ''); const cleanUri = uri.replace(/^\/+/, ''); return `${baseUrl}/${cleanUri}`.replace(/\/+$/, ''); } return `/${uri}`.replace(/\/+$/, '') || '/'; } })(name, params, absolute, config); return router.toString(); }; // Add current() method to check current route window.route.current = function(name) { const currentUrl = window.location.pathname; if (name === undefined) { // Return current route name for (const [routeName, route] of Object.entries(window.Ziggy.routes)) { const pattern = route.uri .replace(/{[^}]+\?}/g, '[^/]*') .replace(/{[^}]+}/g, '[^/]+'); const regex = new RegExp(`^/?${pattern}$`); if (regex.test(currentUrl)) { return routeName; } } return ''; } // Check if current route matches given name const route = window.Ziggy.routes[name]; if (!route) return false; const pattern = route.uri .replace(/{[^}]+\?}/g, '[^/]*') .replace(/{[^}]+}/g, '[^/]+'); const regex = new RegExp(`^/?${pattern}$`); return regex.test(currentUrl); };