skip to Main Content

Hello Masters of the Javascript and Laravel.

I’m wondering if it’s possible to make two separate inertia.js applications in one Laravel application.

I’m making a SaaS style application, where there will be a front facing part and a backoffice part.
This will be split in two domains (or more), using the same Laravel/Octane application. This works.
However, the Ziggy routes from the backoffice also show up in the front facing application.

There’s of course a security risk that these are included, in the Ziggy data. Also transferring the routes from the admin part creates unnecessary traffic, since the routes are transferred with every request with inertia. The backoffice will have a lot more routes than the relatively simple frontend.

Any idea on how this could be structured so the Ziggy data is split in two, while it still being one application?

2

Answers


  1. Chosen as BEST ANSWER

    I ended up doing this in HandleInertiaRequests middleware

     /**
     * Define the props that are shared by default.
     *
     * @return array<string, mixed>
     */
    public function share(Request $request): array
    {
        $ziggyObj = (new Ziggy)->toArray();
    
        if($request->host()!=config('app.admin_domain')) {
            $ziggyObj['routes'] =  array_filter(
                $ziggyObj['routes'],
                fn ($key) => !str_starts_with($key, 'admin'),
                ARRAY_FILTER_USE_KEY
            );
        }
    
        return array_merge(parent::share($request), [
            'auth' => [
                'user' => $request->user(),
            ],
            'ziggy' => function () use ($ziggyObj, $request) {
                return array_merge($ziggyObj, [
                    'location' => $request->url(),
                ]);
            },
        ]);
    }
    

    So any routes that starts with "admin" are not sent to the frontend, unless it's the "admin domain". The front-facing application routes are also sent to the backoffice, but I might filter those out later.


  2. I dont think this would be possible or if it is, it would be too complex setup since Interia relies with Laravel routing and when you are passing Laravel back to interia you’d have to determine which of that Inertia App you’d push the data.

    I think the better approach for this is to just create domain group routes if you need multiple domains support, then create a persistent layout in the front-end which you can use for public/front-end area and admin area. You can just use Laravel Jetstream with Vue+Inertia.

    I dont really see any reason why you would need different front-end application, but if you really do, I think you better just set-up Laravel for API routes so you can set-up different application for the front-end.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search