From e9cfcbca66da250ff19b68c4dfd0228d0d766718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn?= Date: Tue, 15 Oct 2019 19:45:45 +0200 Subject: [PATCH] 0.1.0 --- README.md | 127 +++++++++++++++++++++++++++++++++++++++++++++++--- composer.json | 4 +- 2 files changed, 123 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d00107f..0bce987 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,126 @@ -# Gear - Directus PHP Client +# Super Gear Directus -Small Libary to request Directus API. +Small Libary to request Directus API. Works with [http://flightphp.com/](Flight). -## ItemCollection +## Install -**URL** -**Token** +```bash +composer require tentakelfabrik/super-gear-directus +``` + +## Requirements + +For handle token and url for the Directus Server you have to use +[https://github.com/vlucas/phpdotenv](vlucas/phpdotenv), + +```php +env('DIRECTUS_API_URL'), +env('DIRECTUS_API_TOKEN') +``` + +## Controller + +Example how to use SuperGear\\Directus\\Controllers\\DirectusControllerAbstract, + +```php +class PageController extends DirectusControllerAbstract +{ + /** slug for home */ + const HOME_SLUG = 'home'; + + /** set default view */ + protected $defaultView = 'page/default'; + + /** + * get home page from slug + * + * + */ + public function indexAction() + { + $repository = Manager::get('Page'); + $page = $repository->findOneBySlug(self::HOME_SLUG); + + if ($this->notFound($page)) { + $this->app->redirect('404'); + } + + $this->render($page); + } + + /** + * get single page from slug + * + * + * @param string $slug + */ + public function getAction($slug) + { + $repository = Manager::get('Page'); + $page = $repository->findOneBySlug($slug); + + if ($this->notFound($page)) { + $this->app->redirect('404'); + } + + $this->render($page); + } + + /** + * if page not found + * + */ + public function notFoundAction() + { + $page = [ + 'data' => [ + 'view' => 'page/404' + ] + ]; + + $this->render($page); + } +} +``` + +## Repositories + +Example to use the SuperGear\\Directus\\Respositories\\RepositoryAbstract, ```PHP -$itemCollection = new ItemCollection($url, $token); -$articles = $itemCollection->find('articles', ['status' => 'published']); +class PageRepository extends RepositoryAbstract +{ + /** name of the collection */ + protected $name = 'page'; + + /** + * find single page with a slug, + * page must be published + * + * @param string $slug + * @return array + */ + public function findOneBySlug($slug) + { + return $this->itemCollection->findOne($this->name, [ + 'filter[slug][eq]' => $slug, + 'filter[status][eq]' => 'published' + ]); + } + + /** + * find single page with a slug, + * page must be published + * + * @param string $slug + * @return array + */ + public function findByView($view) + { + return $this->itemCollection->find($this->name, [ + 'filter[view][eq]' => $view, + 'filter[status][eq]' => 'published' + ]); + } +} ``` diff --git a/composer.json b/composer.json index 2fab658..d48a4ff 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,9 @@ "require": { "php": "^7.0", "mikecao/flight": "^1.3", - "erusev/parsedown": "^1.7" + "erusev/parsedown": "^1.7", + "vlucas/phpdotenv": "^3.3", + "jenssegers/blade": "^1.2" }, "autoload": { "psr-4": {