Перевод защищённой части приложения на Vue.js + Inertia

Дашборд, зоны, устройства, правила автоматизации и профиль теперь
Vue 3 + Inertia вместо Blade+Alpine — гостевые страницы Breeze
(логин/регистрация) сознательно оставлены на Blade, чтобы не трогать
уже протестированную аутентификацию. Контроллеры возвращают
Inertia::render() вместо view(), переиспользуют существующие
API Resources и FormRequest.

Ключевой нюанс: Resource/ResourceCollection, переданные напрямую в
проп Inertia, заворачиваются в ключ "data" (Inertia вызывает
toResponse() у Responsable-объектов) — везде используется ->resolve()
и явное резолвление вложенных ресурсов (zone/device_type внутри
Device и т.п.), иначе вложенные поля тоже задваивались бы обёрткой.

Alpine.js и все Blade-вьюхи защищённой части удалены как мёртвый код.
Тесты (ZoneControllerTest/DeviceControllerTest) переведены на
assertInertia(). UserFactory теперь явно задаёт role=owner — без
этого Eloquent не подтягивает DB-default обратно в модель после
create(), что уронило бы общий auth.user проп на любой странице.
This commit is contained in:
2026-08-12 10:52:04 +05:00
parent e739ef5d38
commit be1f238f89
71 changed files with 1988 additions and 1232 deletions
@@ -2,12 +2,18 @@
namespace App\Http\Controllers;
use App\Enums\ConditionOperator;
use App\Enums\DeviceCategory;
use App\Http\Requests\AutomationRuleRequest;
use App\Http\Resources\AutomationRuleResource;
use App\Http\Resources\DeviceResource;
use App\Http\Resources\ZoneResource;
use App\Models\AutomationRule;
use App\Models\Device;
use App\Models\DeviceType;
use App\Models\Zone;
use Illuminate\Support\Collection;
use Inertia\Inertia;
class AutomationRuleController extends Controller
{
@@ -19,14 +25,16 @@ class AutomationRuleController extends Controller
->orderByDesc('id')
->get();
return view('automation-rules.index', ['rules' => $rules]);
return Inertia::render('AutomationRules/Index', [
'rules' => AutomationRuleResource::collection($rules)->resolve(),
]);
}
public function create()
{
$this->authorize('create', AutomationRule::class);
return view('automation-rules.create', $this->formOptions());
return Inertia::render('AutomationRules/Create', $this->formOptions());
}
public function store(AutomationRuleRequest $request)
@@ -42,8 +50,19 @@ class AutomationRuleController extends Controller
{
$this->authorize('update', $automation_rule);
return view('automation-rules.edit', [
'rule' => $automation_rule,
return Inertia::render('AutomationRules/Edit', [
'rule' => [
'id' => $automation_rule->id,
'zone_id' => $automation_rule->zone_id,
'condition_source_device_id' => $automation_rule->condition_source_device_id,
'condition_sensor_type' => $automation_rule->condition_sensor_type,
'condition_operator' => $automation_rule->condition_operator->value,
'condition_value' => $automation_rule->condition_value,
'target_device_id' => $automation_rule->target_device_id,
'action_type' => $automation_rule->action_type,
'level' => $automation_rule->action_params['level'] ?? '',
'is_active' => $automation_rule->is_active,
],
...$this->formOptions(),
]);
}
@@ -91,10 +110,11 @@ class AutomationRuleController extends Controller
$devices = Device::with(['zone', 'deviceType'])->orderBy('name')->get();
return [
'zones' => Zone::orderBy('name')->get(),
'devices' => $devices,
'zones' => ZoneResource::collection(Zone::orderBy('name')->get())->resolve(),
'devices' => DeviceResource::collection($devices)->resolve(),
'actionTypeOptions' => $this->capabilityOptions(DeviceCategory::Actuator),
'sensorTypeOptions' => $this->capabilityOptions(DeviceCategory::Sensor),
'conditionOperatorOptions' => array_map(fn ($case) => $case->value, ConditionOperator::cases()),
];
}
@@ -102,7 +122,7 @@ class AutomationRuleController extends Controller
* Distinct capability values across device_types of the given category
* used as friendly select/datalist suggestions, not a hardcoded list.
*
* @return \Illuminate\Support\Collection<int, string>
* @return Collection<int, string>
*/
private function capabilityOptions(DeviceCategory $category)
{
@@ -3,6 +3,9 @@
namespace App\Http\Controllers;
use App\Http\Requests\DeviceRequest;
use App\Http\Resources\DeviceResource;
use App\Http\Resources\DeviceTypeResource;
use App\Http\Resources\ZoneResource;
use App\Models\Device;
use App\Models\DeviceType;
use App\Models\Zone;
@@ -10,6 +13,7 @@ use App\Services\ClickHouseClient;
use App\Services\DeviceControlClient;
use App\Services\DeviceShadow;
use Illuminate\Http\Request;
use Inertia\Inertia;
class DeviceController extends Controller
{
@@ -19,8 +23,11 @@ class DeviceController extends Controller
$devices = Device::with(['zone', 'deviceType'])->orderBy('name')->get();
$statuses = $shadow->statuses($devices->pluck('external_id')->all());
$devices->each(fn (Device $d) => $d->live_status = $statuses[$d->external_id] ?? 'unknown');
return view('devices.index', compact('devices', 'statuses'));
return Inertia::render('Devices/Index', [
'devices' => DeviceResource::collection($devices)->resolve(),
]);
}
public function show(Device $device, DeviceShadow $shadow, ClickHouseClient $clickHouse)
@@ -28,6 +35,8 @@ class DeviceController extends Controller
$this->authorize('view', $device);
$device->load(['zone', 'deviceType']);
$snapshot = $shadow->snapshot($device->external_id);
$device->live_status = $snapshot['status'];
$telemetry = [];
if ($device->deviceType->category->value === 'sensor') {
@@ -39,9 +48,13 @@ class DeviceController extends Controller
);
}
return view('devices.show', [
'device' => $device,
'shadow' => $shadow->snapshot($device->external_id),
return Inertia::render('Devices/Show', [
'device' => (new DeviceResource($device))->resolve(),
'shadow' => [
'last_seen' => $snapshot['last_seen'],
'desired_state' => $snapshot['desired_state'],
'reported_state' => $snapshot['reported_state'],
],
'telemetry' => $telemetry,
]);
}
@@ -50,7 +63,7 @@ class DeviceController extends Controller
{
$this->authorize('create', Device::class);
return view('devices.create', $this->formOptions());
return Inertia::render('Devices/Create', $this->formOptions());
}
public function store(DeviceRequest $request)
@@ -66,7 +79,10 @@ class DeviceController extends Controller
{
$this->authorize('update', $device);
return view('devices.edit', ['device' => $device, ...$this->formOptions()]);
return Inertia::render('Devices/Edit', [
'device' => $device->only(['id', 'name', 'zone_id', 'device_type_id', 'external_id', 'protocol']),
...$this->formOptions(),
]);
}
public function update(DeviceRequest $request, Device $device)
@@ -137,8 +153,8 @@ class DeviceController extends Controller
private function formOptions(): array
{
return [
'zones' => Zone::orderBy('name')->get(),
'deviceTypes' => DeviceType::orderBy('code')->get(),
'zones' => ZoneResource::collection(Zone::orderBy('name')->get())->resolve(),
'deviceTypes' => DeviceTypeResource::collection(DeviceType::orderBy('code')->get())->resolve(),
];
}
}
@@ -7,17 +7,18 @@ use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;
use Inertia\Inertia;
use Inertia\Response;
class ProfileController extends Controller
{
/**
* Display the user's profile form.
*/
public function edit(Request $request): View
public function edit(Request $request): Response
{
return view('profile.edit', [
'user' => $request->user(),
return Inertia::render('Profile/Edit', [
'user' => $request->user()->only(['id', 'name', 'email']),
]);
}
@@ -3,7 +3,9 @@
namespace App\Http\Controllers;
use App\Http\Requests\ZoneRequest;
use App\Http\Resources\ZoneResource;
use App\Models\Zone;
use Inertia\Inertia;
class ZoneController extends Controller
{
@@ -13,14 +15,16 @@ class ZoneController extends Controller
$zones = Zone::withCount('devices')->orderBy('name')->get();
return view('zones.index', compact('zones'));
return Inertia::render('Zones/Index', [
'zones' => ZoneResource::collection($zones)->resolve(),
]);
}
public function create()
{
$this->authorize('create', Zone::class);
return view('zones.create');
return Inertia::render('Zones/Create');
}
public function store(ZoneRequest $request)
@@ -34,7 +38,9 @@ class ZoneController extends Controller
{
$this->authorize('update', $zone);
return view('zones.edit', compact('zone'));
return Inertia::render('Zones/Edit', [
'zone' => (new ZoneResource($zone))->resolve(),
]);
}
public function update(ZoneRequest $request, Zone $zone)