66 lines
2.3 KiB
PHP
66 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middlewares;
|
|
|
|
use Diglactic\Breadcrumbs\Breadcrumbs;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Inertia\Middleware;
|
|
use Uwil\LaravelCommon\Helpers\ToolboxHelper;
|
|
use Uwil\LaravelCommon\Support\InertiaSharedRegistry;
|
|
use Uwil\LaravelCommon\Support\TranslationRegistry;
|
|
|
|
class HandleInertiaRequests extends Middleware
|
|
{
|
|
/**
|
|
* Define the props that are shared by default.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function share(Request $request): array
|
|
{
|
|
// Get translations from translator app.
|
|
$isAdmin = $request->route()->getPrefix() === "admin";
|
|
$excludeKey = $isAdmin ? "front" : "back";
|
|
$translations = array_merge(
|
|
$isAdmin ? TranslationRegistry::resolve("admin") : [],
|
|
Arr::except(ToolboxHelper::getCachedTranslations(), $excludeKey)
|
|
);
|
|
|
|
// Merge global flash error into Inertia's errors bag.
|
|
$shared = parent::share($request);
|
|
if (session('error')) {
|
|
$errors = $shared['errors'] ?? [];
|
|
if (is_callable([$errors, 'toArray'])) {
|
|
$errors = $errors->toArray();
|
|
}
|
|
if (!is_array($errors)) {
|
|
$errors = [];
|
|
}
|
|
$shared['errors'] = array_merge($errors, ['global' => session('error')]);
|
|
}
|
|
|
|
$props = [
|
|
...$shared,
|
|
'appEnv' => [
|
|
'mode' => config('app.env'),
|
|
'name' => config('app.name'),
|
|
'url' => config('app.url'),
|
|
'locale' => app()->currentLocale(),
|
|
'locales' => [app()->currentLocale()],
|
|
'fallbackLocale' => app()->getFallbackLocale(),
|
|
],
|
|
'translations' => $translations,
|
|
];
|
|
if ($request->route() && Breadcrumbs::exists($request->route()->getName())) {
|
|
$props = array_merge($props, [
|
|
'breadcrumbs' => Breadcrumbs::generate($request->route()->getName())->toArray()
|
|
]);
|
|
}
|
|
return array_replace_recursive(
|
|
$props,
|
|
InertiaSharedRegistry::resolve($request)
|
|
);
|
|
}
|
|
}
|