選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

55 行
1.4KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace Core;
  4. use Cartalyst\Sentinel\Native\Facades\Sentinel;
  5. abstract class Controller
  6. {
  7. protected function view(string $view, array $data = []): Response
  8. {
  9. return View::render($view, $data);
  10. }
  11. protected function fragment(string $view, array $data = [], int $status = 200, array $headers = []): Response
  12. {
  13. return View::fragment($view, $data, $status, $headers);
  14. }
  15. protected function redirect(string $url): Response
  16. {
  17. return Response::redirect($url);
  18. }
  19. protected function json(array $data): Response
  20. {
  21. return Response::json($data);
  22. }
  23. protected function requirePost(Request $request): void
  24. {
  25. if ($request->method() !== 'POST') {
  26. throw new \Exception('This action requires POST.');
  27. }
  28. }
  29. protected function requireAuth(): ?Response
  30. {
  31. if (!Sentinel::check()) {
  32. return $this->redirect('/login');
  33. }
  34. return null;
  35. }
  36. protected function fileResponse(string $content, string $filename, string $mimeType = 'application/octet-stream'): Response
  37. {
  38. return new Response($content, 200, [
  39. 'Content-Type' => $mimeType,
  40. 'Content-Disposition' => 'attachment; filename="' . $filename . '"',
  41. 'Content-Length' => (string) strlen($content),
  42. ]);
  43. }
  44. }

Powered by TurnKey Linux.