From f7e411f865fd886ae7ec1cd5ee93866c71b8899c Mon Sep 17 00:00:00 2001 From: HerrHase Date: Sun, 7 Nov 2021 15:30:36 +0100 Subject: [PATCH] adding #12 --- app/Repositories/MenuRepository.php | 66 ++++++++++++++++++++++++++ app/Repositories/SiteRepository.php | 6 ++- app/Repositories/SnippetRepository.php | 46 ++++++++++++++++++ public/index.php | 11 ++++- storage/.gitkeep | 0 5 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 app/Repositories/MenuRepository.php create mode 100644 app/Repositories/SnippetRepository.php create mode 100644 storage/.gitkeep diff --git a/app/Repositories/MenuRepository.php b/app/Repositories/MenuRepository.php new file mode 100644 index 0000000..1c002fa --- /dev/null +++ b/app/Repositories/MenuRepository.php @@ -0,0 +1,66 @@ +queryBuilder + ->fields([ + 'title', + 'target', + 'url', + 'page.title', + 'page.status', + 'page.slug', + 'menus.sort', + 'menus.menus_id.name' + ]) + ->aliases('page[title]', 'page_title') + ->aliases('page[status]', 'page_status') + ->aliases('page[slug]', 'page_slug') + ->filter([ + 'page' => [ + 'status' => 'published' + ], + 'menus' => [ + 'menus_id' => [ + 'name' => $name, + 'status' => 'published' + ] + ] + ]) + ->find(); + + // @TODO Workaround sort functions seems have problems with relationals fields + if (count($results['data'])) { + usort($results['data'], function($a, $b) { + return ($a['menus'][0]['sort'] > $b['menus'][0]['sort']); + }); + } + + return $results; + } +} diff --git a/app/Repositories/SiteRepository.php b/app/Repositories/SiteRepository.php index f51a3e2..19d94be 100644 --- a/app/Repositories/SiteRepository.php +++ b/app/Repositories/SiteRepository.php @@ -5,7 +5,7 @@ namespace App\Repositories; use App\Repositories\RepositoryAbstract; /** - * request pages items from directus + * request site singleton * * @author Björn Hase, Tentakelfabrik * @license http://opensource.org/licenses/MIT The MIT License @@ -29,7 +29,9 @@ class SiteRepository extends RepositoryAbstract { return $this->queryBuilder ->fields([ - 'title', 'description', 'logo' + 'title', + 'description', + 'logo' ]) ->aliases('logo[id]', 'logo') ->findOne(); diff --git a/app/Repositories/SnippetRepository.php b/app/Repositories/SnippetRepository.php new file mode 100644 index 0000000..5e0513e --- /dev/null +++ b/app/Repositories/SnippetRepository.php @@ -0,0 +1,46 @@ +queryBuilder + ->fields([ + 'title', + 'content', + 'view', + 'blocks', + 'files.directus_files_id' + ]) + ->aliases('template', 'view') + ->filter([ + 'type' => $type + ]) + ->find(); + + return $results; + } +} diff --git a/public/index.php b/public/index.php index 9b2d87d..b6499e5 100644 --- a/public/index.php +++ b/public/index.php @@ -3,7 +3,16 @@ require __DIR__.'/../vendor/autoload.php'; require __DIR__.'/../app/bootstrap.php'; -// default routes +// make sure that all routes with a slug and ending with a "/" redirect to a route without +$flight->route('GET /@slug:[a-z0-9\-]+/$', function() use ($flight) { + + // getting position and build route + $position = strlen($flight->request()->url) - 1; + $route = substr_replace($flight->request()->url, '', $position, 1); + + $flight->redirect($route); +}); + $flight->route('GET /404', array(new App\Controllers\PageController, 'notFoundAction')); $flight->route('GET /(@slug:[a-z0-9\-]+$)', array(new App\Controllers\PageController, 'getAction')); diff --git a/storage/.gitkeep b/storage/.gitkeep new file mode 100644 index 0000000..e69de29