inertia-laravel@v0.3.0

Published on October 14, 2020

Creating new middleware

This release removes the automatic registration of the Inertia middleware. Instead, there is a new command for generating your own HandleInertiaRequests middleware:

php artisan inertia:middleware

Registering new middleware

Once you've generated the new middleware, be sure to register the HandleInertiaRequests middleware in your App\Http\Kernel file as the last item in your web middleware group.

protected $middlewareGroups = [
    'web' => [
        ...
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
+       \App\Http\Middleware\HandleInertiaRequests::class,
    ],

Using the new middleware

This middleware provides a version() method for setting your asset version, and a share() method for setting the props that are shared by default. This provides a better approach than doing this in your AppServiceProvider using the Inertia::version() and Inertia::share() helpers.

Here's the complete example of how to use the new HandleInertiaRequests middleware:

<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Inertia\Middleware;

class HandleInertiaRequests extends Middleware
{
    /**
     * Determines the current asset version.
     *
     * @see https://inertiajs.com/asset-versioning
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    public function version(Request $request)
    {
        return parent::version($request);
    }

    /**
     * Defines the props that are shared by default.
     *
     * @see https://inertiajs.com/shared-data
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function share(Request $request)
    {
        return array_merge(parent::share($request), [
            'auth' => fn () => [
                'user' => optional($request->user())->only(['name', 'email']),
            ],
            'flash' => fn () => $request->session()->only(['success', 'error']),
        ]);
    }
}