Boilerplate to use a Directus Instance to Build a Custom Website, Content will be Manage by Directus
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Controllers;
  3. use App\Flight\FlightAbstract;
  4. use Exception;
  5. /**
  6. * abstract controller to handle views and response from directus
  7. *
  8. *
  9. * @author Björn Hase, Tentakelfabrik
  10. * @license http://opensource.org/licenses/MIT The MIT License
  11. * @link https://gitea.tentakelfabrik.de/Tentakelfabrik/super-gear-directus
  12. *
  13. */
  14. abstract class DirectusControllerAbstract extends FlightAbstract
  15. {
  16. /** default template for view */
  17. protected $defaultView = NULL;
  18. /**
  19. * if item not found
  20. *
  21. * @param string $page
  22. * @return boolean
  23. */
  24. protected function notFound($item)
  25. {
  26. return (!$item || ($item && isset($item['error']) && $item['error']['code'] === 203));
  27. }
  28. /**
  29. *
  30. * @param string $view
  31. * @return boolean
  32. */
  33. protected function viewExists($view)
  34. {
  35. $result = false;
  36. if (file_exists($this->app->get('flight.views.path').'/'.$view.'.blade.php')) {
  37. $result = true;
  38. }
  39. return $result;
  40. }
  41. /**
  42. *
  43. * @param [type] $page [description]
  44. * @param array $data [description]
  45. * @return [type] [description]
  46. */
  47. protected function render($page, $data = [])
  48. {
  49. $view = $this->defaultView;
  50. // if view isset in page and file exists
  51. if (isset($page['data']['view'])) {
  52. if ($this->viewExists($page['data']['view'])) {
  53. $view = $page['data']['view'];
  54. } else {
  55. throw new Exception('View '.$page['data']['view'].' not exists');
  56. }
  57. } else if (!$this->viewExists($view)) {
  58. throw new Exception('View '.$view.' not exists');
  59. }
  60. $this->app->render($view, array_merge([
  61. 'page' => $page,
  62. 'flight' => $this->app
  63. ],
  64. $data
  65. ));
  66. }
  67. }