- Adjusted indentation and formatting in login, permissions, profile, and security pages for better readability. - Enhanced the clarity of conditional statements and function calls in permissions and profile components. - Updated type definitions in vite-env.d.ts for better code structure. - Cleaned up array mapping syntax in ProductTest.php for consistency.
30 lines
1.0 KiB
PHP
30 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Inertia\Inertia;
|
|
|
|
abstract class Controller
|
|
{
|
|
protected function handleAction(callable $action, string $successMessage, string $redirectRoute, ?string $errorRoute = null, array $parameters = []): RedirectResponse
|
|
{
|
|
try {
|
|
$action();
|
|
Inertia::flash('toast', ['type' => 'success', 'message' => $successMessage]);
|
|
|
|
return to_route($redirectRoute, $parameters);
|
|
} catch (ValidationException $e) {
|
|
$firstError = collect($e->errors())->flatten()->first();
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $firstError ?? 'Terjadi kesalahan.']);
|
|
|
|
return to_route($errorRoute ?? $redirectRoute, $parameters);
|
|
} catch (\Exception $e) {
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $e->getMessage()]);
|
|
|
|
return to_route($errorRoute ?? $redirectRoute, $parameters);
|
|
}
|
|
}
|
|
}
|