From b676c952270407aed7a381f899749e5016ef5edb Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 10 Jul 2019 21:03:53 +0200 Subject: [PATCH] #2878 --- .../Http/Controllers/CubistPageController.php | 19 +++++++++++++++++ src/app/Magic/Menu/Item.php | 18 ++++++++++++++++ src/app/Magic/Menu/Menu.php | 21 +++++++++++++------ 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/app/Http/Controllers/CubistPageController.php b/src/app/Http/Controllers/CubistPageController.php index 5f3f00d..833b525 100644 --- a/src/app/Http/Controllers/CubistPageController.php +++ b/src/app/Http/Controllers/CubistPageController.php @@ -2,6 +2,7 @@ namespace Cubist\Backpack\app\Http\Controllers; +use Cubist\Backpack\app\Magic\Menu\Menu; use Cubist\Backpack\app\Magic\Models\CMSPage; use Illuminate\Support\Str; @@ -23,4 +24,22 @@ class CubistPageController extends CubistFrontController return view('pages.' . $page->template, $this->data); } + + public function catchall($main = '', $subs = []) + { + $path = trim($main . '/' . implode($subs), '/'); + if (!$path) { + $path = 'home'; + } + + $nav = Menu::getNavigation(); + $item = $nav->findItemByHref($path); + if (null === $item) { + $this->_404(); + } + + $c = $item->getController(); + + redirect()->action($c['controller'] . '@' . $c['action'], $c['params']); + } } diff --git a/src/app/Magic/Menu/Item.php b/src/app/Magic/Menu/Item.php index d87a894..0d8e21b 100644 --- a/src/app/Magic/Menu/Item.php +++ b/src/app/Magic/Menu/Item.php @@ -95,6 +95,24 @@ class Item $this->addChild($child); } + + public function findItemByHref($href) + { + if ($this->getHref() == $href) { + return $this; + } + if (!$this->hasChildren()) { + return null; + } + foreach ($this->getChildren() as $child) { + $res = $child->findItemByHref($href); + if (null !== $res) { + return $res; + } + } + return null; + } + /** * @return string */ diff --git a/src/app/Magic/Menu/Menu.php b/src/app/Magic/Menu/Menu.php index 7f5fe70..fe83057 100644 --- a/src/app/Magic/Menu/Menu.php +++ b/src/app/Magic/Menu/Menu.php @@ -6,7 +6,10 @@ use Lavary\Menu\Menu as BaseMenu; class Menu extends BaseMenu { - protected $_nav = null; + /** + * @var Item + */ + protected static $_nav = null; public function get($key) { @@ -16,16 +19,22 @@ class Menu extends BaseMenu return parent::get($key); } - public function getNavigation() + /** + * @return Item + */ + public static function getNavigation() { - $nav = new Item(); - $nav->initFromDatabase(); - return $nav; + if (self::$_nav === null) { + self::$_nav = new Item(); + self::$_nav->initFromDatabase(); + } + return self::$_nav; + } public function makeAllMenus() { - $nav = $this->getNavigation(); + $nav = self::getNavigation(); foreach ($nav->getChildren() as $main) { $this->make($main->getName(), function ($menu) use ($main) { $main->makeMenu($menu); -- 2.39.5