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.

126 lines
2.5 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
  1. # Super Gear Directus
  2. Small Libary to request Directus API. Works with [http://flightphp.com/](Flight).
  3. ## Install
  4. ```bash
  5. composer require tentakelfabrik/super-gear-directus
  6. ```
  7. ## Requirements
  8. For handle token and url for the Directus Server you have to use
  9. [https://github.com/vlucas/phpdotenv](vlucas/phpdotenv),
  10. ```php
  11. env('DIRECTUS_API_URL'),
  12. env('DIRECTUS_API_TOKEN')
  13. ```
  14. ## Controller
  15. Example how to use SuperGear\\Directus\\Controllers\\DirectusControllerAbstract,
  16. ```php
  17. class PageController extends DirectusControllerAbstract
  18. {
  19. /** slug for home */
  20. const HOME_SLUG = 'home';
  21. /** set default view */
  22. protected $defaultView = 'page/default';
  23. /**
  24. * get home page from slug
  25. *
  26. *
  27. */
  28. public function indexAction()
  29. {
  30. $repository = Manager::get('Page');
  31. $page = $repository->findOneBySlug(self::HOME_SLUG);
  32. if ($this->notFound($page)) {
  33. $this->app->redirect('404');
  34. }
  35. $this->render($page);
  36. }
  37. /**
  38. * get single page from slug
  39. *
  40. *
  41. * @param string $slug
  42. */
  43. public function getAction($slug)
  44. {
  45. $repository = Manager::get('Page');
  46. $page = $repository->findOneBySlug($slug);
  47. if ($this->notFound($page)) {
  48. $this->app->redirect('404');
  49. }
  50. $this->render($page);
  51. }
  52. /**
  53. * if page not found
  54. *
  55. */
  56. public function notFoundAction()
  57. {
  58. $page = [
  59. 'data' => [
  60. 'view' => 'page/404'
  61. ]
  62. ];
  63. $this->render($page);
  64. }
  65. }
  66. ```
  67. ## Repositories
  68. Example to use the SuperGear\\Directus\\Respositories\\RepositoryAbstract,
  69. ```PHP
  70. class PageRepository extends RepositoryAbstract
  71. {
  72. /** name of the collection */
  73. protected $name = 'page';
  74. /**
  75. * find single page with a slug,
  76. * page must be published
  77. *
  78. * @param string $slug
  79. * @return array
  80. */
  81. public function findOneBySlug($slug)
  82. {
  83. return $this->itemCollection->findOne($this->name, [
  84. 'filter[slug][eq]' => $slug,
  85. 'filter[status][eq]' => 'published'
  86. ]);
  87. }
  88. /**
  89. * find single page with a slug,
  90. * page must be published
  91. *
  92. * @param string $slug
  93. * @return array
  94. */
  95. public function findByView($view)
  96. {
  97. return $this->itemCollection->find($this->name, [
  98. 'filter[view][eq]' => $view,
  99. 'filter[status][eq]' => 'published'
  100. ]);
  101. }
  102. }
  103. ```