BaseController.php 711 B

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