| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\File;
- class FormController extends Controller
- {
- public function index()
- {
- return view('form');
- }
- public function submit(Request $request)
- {
- $validated = $request->validate([
- 'name' => 'required|string|min:3|max:15',
- 'email' => 'required|email|max:50',
- 'message' => 'required|string|min:10|max:100'
- ]);
- $path = storage_path('app/forms/contacts.json');
- File::ensureDirectoryExists(dirname($path));
- $data = [];
- if (File::exists($path)) {
- $content = File::get($path);
- $data = json_decode($content, true) ?: [];
- }
- $validated['created_at'] = now()->toDateTimeString();
- $data[] = $validated;
- File::put($path, json_encode($data, JSON_UNESCAPED_UNICODE));
- return redirect()->back()->with('success', 'Данные сохранены!');
- }
- }
|