Did you add ziggy middleware before inertia middleware?
Filtering routes with Ziggy and Inertia
As we all know, Ziggy exposes all your routes in the html. I don't mind the routes being exposed to authenticated users, but I would like to hide them from guests.
So basically, I'm looking for a way to only expose the /login and /forgot-password routes to unauthenticated users. Then once they login, all routes can be exposed again.
I'm playing around with doing this in a middleware:
// app/Http/Middleware/Ziggy.php
class Ziggy
{
protected $guestRoutes = [
'login',
'password.request',
'password.email',
'password.update',
'password.reset',
];
public function handle(Request $request, Closure $next)
{
if (auth()->guest()) {
config(['ziggy.only' => $this->guestRoutes]);
}
return $next($request);
}
}
// app/Http/Kernel.php
class Kernel extends HttpKernel
{
//...
protected $middlewareGroups = [
'web' => [
//...
\App\Http\Middleware\HandleInertiaRequests::class,
\App\Http\Middleware\Test::class,
],
];
}
This kind of works... if you manually refresh the page, but it won't work if you login with Inertia.
Any idea how I might get this to work?
I found a better way to do this, here's instructions:
- Remove the
@routesblade directive fromresources/views/app.blade.php. - Install
npm install ziggy-js - Removed everything related to ziggy in
resources/js/app.js - Defined your "groups" in
app/config/ziggy.php.
return [
'groups' => [
'guest' => [
'login', 'password.request', 'password.email',
'password.reset', 'password.update',
],
'auth' => ['*'],
],
];
- Update
app/Http/Middleware/HandleInertiaRequests.phpwith logic to determine what "group" to filter by.
//..
use Tightenco\Ziggy\Ziggy;
class HandleInertiaRequests extends Middleware
{
// ...
public function share(Request $request): array
{
$array = array_merge(parent::share($request), [
'ziggy' => (new Ziggy('guest'))->toArray(),
]);
if (auth()->guest()) {
return $array;
}
return array_merge($array, [
'ziggy' => (new Ziggy('auth'))->toArray(),
]);
}
}
- Create a new "route" composable:
resources/js/Composables/Route.js
import { usePage } from '@inertiajs/inertia-vue3'
import route from 'ziggy-js'
export default function useRoute(
name, params, absolute,
config = usePage().props.value.ziggy
) {
return route(name, params, absolute, config)
}
That's it! Example usage:
<template>
<AppLayout>
<div v-for="project in projects" :key="project.id">
<Link :href="route('projects.show', project)">
{{ project.name }}
</Link>
</div>
</AppLayout>
</template>
<script setup>
import { Link } from '@inertiajs/inertia-vue3'
import route from '@/Composables/Route'
import AppLayout from '@/Layouts/AppLayout.vue'
defineProps({
projects: Array,
})
</script>
Notes
-
If anyone knows how to use this globally, without having to import in every file, please let me know.
-
On step 5, I'm not sure how expensive it is to make these calls. You may want to consider caching them.
'ziggy' => (new Ziggy('guest'))->toArray() // cache this?
'ziggy' => (new Ziggy('auth'))->toArray() // cache this?
Please or to participate in this conversation.