Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share routes with front frameworks like Inertia/VueJS #68

Open
kira0269 opened this issue Jan 6, 2023 · 4 comments
Open

Share routes with front frameworks like Inertia/VueJS #68

kira0269 opened this issue Jan 6, 2023 · 4 comments

Comments

@kira0269
Copy link

kira0269 commented Jan 6, 2023

Hello !

Is it possible to generate routes correctly with Inertia using your package ?

With Inertia, the Ziggy library is used. After debugging, my route list contains for example:

  • en.
  • en.dashboard
  • en.list.
  • ...

And calling route('dashboard') function in inertia does not work.

@ivanvermeyen
Copy link
Contributor

Hi,

This package overrides Laravel's UrlGenerator to apply the locale prefix to the requested route name.
I haven't used inertia yet, but I can see that Ziggy uses a JS route() helper. I can't really tell however how they get the routes from the backend to the frontend...

I assume that the JS helper doesn't use the backend, thus the locale prefix isn't applied.

@cv-chameleon
Copy link

cv-chameleon commented Mar 20, 2023

@kira0269

Your problem is quite easy to fix.

In your app.js you need to add a mixin, for example "$route", which passes your locale (you can pass add this to your props in HandleInertiaRequests), to the default route function.

VueApp .use(plugin).mixin({ methods: { $route: (name, params, absolute, config) => route(${usePage().props.locale}.${name}, params, absolute, config) } }).mount(el) return VueApp;

Then in your Vue components, instead of using route('homepage'), you now use $route('homepage'), which will be transformed to for example route('en.homepage').

@Baspa
Copy link

Baspa commented Aug 21, 2023

I am currently using React with Inertia. I use the react-i18n-next for the translations package to translate strings in the frontend. When using the route() helper from Ziggy I get the translation like this:

...
import { useTranslation } from "react-i18next";

export default () => {
    const { i18n } = useTranslation();

    const localizedRoute = route(i18n.language + ".index");

    return (
        <>
       
        </>
    );
}

The React package knows exactly which translation is being used so it always shows the correct localized route.

@desaintflorent
Copy link

How I solved it with vuejs inertiajs + ssr following @cv-chameleon help

//HandleInertiaRequests.php
//...
 return [
    ...parent::share($request),
    'locale' => App::currentLocale(),
    'auth' => [
        'user' => $request->user(),
    ],
    'ziggy' => fn () => [
        ...(new Ziggy)->toArray(),
        'location' => $request->url(),
    ],
];
//app.js
import { createInertiaApp, usePage } from "@inertiajs/vue3";
//...
.mixin({
    methods: {
        $route: (name, params, absolute, config) =>
            route(
                `${usePage().props.locale}.${name}`,
                params,
                absolute,
                config
            ),
    },
})
//ssr.js
import { route, ZiggyVue } from "../../vendor/tightenco/ziggy";
//...
setup({ App, props, plugin }) {
    const Ziggy = {
        ...page.props.ziggy,
        location: new URL(page.props.ziggy.location),
    };

    return createSSRApp({ render: () => h(App, props) })
        .use(plugin)
        .mixin({
            methods: {
                $route: (name, params, absolute, config = Ziggy) =>
                    route(
                        `${page.props.locale}.${name}`,
                        params,
                        absolute,
                        config
                    ),
            },
        })
        .use(ZiggyVue, Ziggy);
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants