Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

luismabenitez's avatar

Ziggy, InertiaJS & Vite : route is not defined

I removed the @routes from the main blade file and everything works fine but in the vue components inside the section.

My app.js file has this:

import { ZiggyVue } from '../../vendor/tightenco/ziggy/dist/vue.m';
import { Ziggy } from './ziggy.js';

Then:

createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
    setup({ el, app, props, plugin }) {
        return createApp({ render: () => h(app, props) })
            .use(plugin)
            .use(ZiggyVue, Ziggy)
            .component('InertiaHead', Head)
            .component('InertiaLink', Link)
            .mount(el);
    },
});

My vite.config.js file:

export default defineConfig({
    resolve: {
        alias: {
            'ziggy': '/vendor/tightenco/ziggy/src/js',
            'ziggy-vue': '/vendor/tightenco/ziggy/dist/vue',
            '@': '/resources/js',
        },
    },
    optimizeDeps: {
        include: ["ziggy-vue", "ziggy"],
    },
...

Everything works fine on my Vue templates, but I got this js error (Uncaught ReferenceError: route is not defined) when I use the route() inside the on something like:

Inertia.post(route('logout'));
0 likes
6 replies
vincent15000's avatar

In my configuration, I haven't anything about Ziggy in the vite configuration, and I don't have anything in the app.js and bootstrap.js files.

I have installed Ziggy with composer and it works immediately (after having written @routes in the app.blade.php file).

Sinnbeck's avatar

@luismabenitez if you don't want any routes exposed, simply don't use ziggy. Ziggy is a tool for exposing routes

2 likes
vincent15000's avatar

@luismabenitez If you don't want any route to be exposed, Ziggy is not the good package to do that.

Before testing Ziggy, I had the idea to write a configuration file where I rewrite my routes. And the aim was to retrieve the routes in a controller and send them to the front. Each route could have some attributes like the route name, the route URL, some conditions to send the route to the front (admin, member, ...), ...

Even by this way, once the routes have been sent to the front, they are written in your menu bar, ... and are anyway exposed.

If you need to work with only the routes names in order to not expose the routes URL, I don't any other way that not writing the front with any JS framework, so only with blade. I don't really know SSR (server side rendering) but it's perhaps a solution for you, InertiaJS propose an SSR functionality.

Please or to participate in this conversation.