Use Inertia Middleware for bootstrapping (#88)
* Use Inertia Middleware for bootstrapping * Update Middleware * WIP * Rename middleware to new defaults * Fix middleware in Kernel * Align Middleware + update dependency Co-authored-by: Claudio Dekker <claudiodekker@users.noreply.github.com>
This commit is contained in:
parent
c322e813f0
commit
39cd397852
|
@ -37,6 +37,7 @@ class Kernel extends HttpKernel
|
||||||
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
|
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
|
||||||
\App\Http\Middleware\VerifyCsrfToken::class,
|
\App\Http\Middleware\VerifyCsrfToken::class,
|
||||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||||
|
\App\Http\Middleware\HandleInertiaRequests::class
|
||||||
],
|
],
|
||||||
|
|
||||||
'api' => [
|
'api' => [
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Inertia\Middleware;
|
||||||
|
|
||||||
|
class HandleInertiaRequests extends Middleware
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The root template that's loaded on the first page visit.
|
||||||
|
*
|
||||||
|
* @see https://inertiajs.com/server-side-setup#root-template
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $rootView = 'app';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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' => function () use ($request) {
|
||||||
|
return [
|
||||||
|
'user' => $request->user() ? [
|
||||||
|
'id' => $request->user()->id,
|
||||||
|
'first_name' => $request->user()->first_name,
|
||||||
|
'last_name' => $request->user()->last_name,
|
||||||
|
'email' => $request->user()->email,
|
||||||
|
'role' => $request->user()->role,
|
||||||
|
'account' => [
|
||||||
|
'id' => $request->user()->account->id,
|
||||||
|
'name' => $request->user()->account->name,
|
||||||
|
],
|
||||||
|
] : null,
|
||||||
|
];
|
||||||
|
},
|
||||||
|
'flash' => function () use ($request) {
|
||||||
|
return [
|
||||||
|
'success' => $request->session()->get('success'),
|
||||||
|
'error' => $request->session()->get('error'),
|
||||||
|
];
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,12 +5,9 @@ namespace App\Providers;
|
||||||
use Illuminate\Pagination\LengthAwarePaginator;
|
use Illuminate\Pagination\LengthAwarePaginator;
|
||||||
use Illuminate\Pagination\UrlWindow;
|
use Illuminate\Pagination\UrlWindow;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\Auth;
|
|
||||||
use Illuminate\Support\Facades\Request;
|
use Illuminate\Support\Facades\Request;
|
||||||
use Illuminate\Support\Facades\Session;
|
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Inertia\Inertia;
|
|
||||||
use League\Glide\Server;
|
use League\Glide\Server;
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
||||||
|
@ -22,42 +19,10 @@ class AppServiceProvider extends ServiceProvider
|
||||||
*/
|
*/
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
$this->registerInertia();
|
|
||||||
$this->registerGlide();
|
$this->registerGlide();
|
||||||
$this->registerLengthAwarePaginator();
|
$this->registerLengthAwarePaginator();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function registerInertia()
|
|
||||||
{
|
|
||||||
Inertia::version(function () {
|
|
||||||
return md5_file(public_path('mix-manifest.json'));
|
|
||||||
});
|
|
||||||
|
|
||||||
Inertia::share([
|
|
||||||
'auth' => function () {
|
|
||||||
return [
|
|
||||||
'user' => Auth::user() ? [
|
|
||||||
'id' => Auth::user()->id,
|
|
||||||
'first_name' => Auth::user()->first_name,
|
|
||||||
'last_name' => Auth::user()->last_name,
|
|
||||||
'email' => Auth::user()->email,
|
|
||||||
'role' => Auth::user()->role,
|
|
||||||
'account' => [
|
|
||||||
'id' => Auth::user()->account->id,
|
|
||||||
'name' => Auth::user()->account->name,
|
|
||||||
],
|
|
||||||
] : null,
|
|
||||||
];
|
|
||||||
},
|
|
||||||
'flash' => function () {
|
|
||||||
return [
|
|
||||||
'success' => Session::get('success'),
|
|
||||||
'error' => Session::get('error'),
|
|
||||||
];
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function registerGlide()
|
protected function registerGlide()
|
||||||
{
|
{
|
||||||
$this->app->bind(Server::class, function ($app) {
|
$this->app->bind(Server::class, function ($app) {
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
"fruitcake/laravel-cors": "^2.0",
|
"fruitcake/laravel-cors": "^2.0",
|
||||||
"fzaninotto/faker": "^1.9.1",
|
"fzaninotto/faker": "^1.9.1",
|
||||||
"guzzlehttp/guzzle": "^7.0.1",
|
"guzzlehttp/guzzle": "^7.0.1",
|
||||||
"inertiajs/inertia-laravel": "^0.2",
|
"inertiajs/inertia-laravel": "^0.3",
|
||||||
"laravel/framework": "^8.0",
|
"laravel/framework": "^8.0",
|
||||||
"laravel/legacy-factories": "^1.0",
|
"laravel/legacy-factories": "^1.0",
|
||||||
"laravel/tinker": "^2.0",
|
"laravel/tinker": "^2.0",
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue