BaseController.php 879 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Base;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Arr;
  5. class BaseController extends Controller
  6. {
  7. /**
  8. * 不支持多维结构
  9. *
  10. * @param array $rules
  11. * @param array $messages
  12. * @param array $customAttributes
  13. * @return array
  14. */
  15. public function valid(array $rules, array $messages = [], array $customAttributes = []): array
  16. {
  17. $data = [];
  18. $validated = request()->validate($rules);
  19. foreach ($rules as $k => $v) {
  20. $data[$k] = Arr::get($validated, $k);
  21. }
  22. return $data;
  23. }
  24. public function ok($data = null)
  25. {
  26. return [
  27. "code" => 200,
  28. "data" => $data,
  29. ];
  30. }
  31. public function error(string $msg)
  32. {
  33. return [
  34. "code" => 400,
  35. "error" => $msg,
  36. ];
  37. }
  38. }