FormController.php 1019 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\File;
  5. class FormController extends Controller
  6. {
  7. public function index()
  8. {
  9. return view('form');
  10. }
  11. public function submit(Request $request)
  12. {
  13. $validated = $request->validate([
  14. 'name' => 'required|string|min:3|max:15',
  15. 'email' => 'required|email|max:50',
  16. 'message' => 'required|string|min:10|max:100'
  17. ]);
  18. $path = storage_path('app/forms/contacts.json');
  19. File::ensureDirectoryExists(dirname($path));
  20. $data = [];
  21. if (File::exists($path)) {
  22. $content = File::get($path);
  23. $data = json_decode($content, true) ?: [];
  24. }
  25. $validated['created_at'] = now()->toDateTimeString();
  26. $data[] = $validated;
  27. File::put($path, json_encode($data, JSON_UNESCAPED_UNICODE));
  28. return redirect()->back()->with('success', 'Данные сохранены!');
  29. }
  30. }