diff --git a/resources/Hbs/App/routerType.hbs b/resources/Hbs/App/routerType.hbs index acceed2..4fe7ac5 100644 --- a/resources/Hbs/App/routerType.hbs +++ b/resources/Hbs/App/routerType.hbs @@ -45,13 +45,13 @@ class {{Class}} implements CrazyRouterType { /** * Parser Reg Exp * - * Method returns regexp to parse the language code if it occurs + * Method returns regexp to parse the code if it occurs * * @return string regexp for parsing */ public static function parserRegExp():string { - # Regular expression for ISO 639-1 standard language codes + # Regular expression return '{{Regex}}'; } diff --git a/src/Core/Router.php b/src/Core/Router.php index 1bfe02e..8fc5747 100644 --- a/src/Core/Router.php +++ b/src/Core/Router.php @@ -18,6 +18,7 @@ use CrazyPHP\Library\Router\Router as LibraryRouter; use Mezon\Router\Types\BaseType as VendorBaseType; use Mezon\Router\Router as VendorRouter; +use CrazyPHP\Interface\CrazyRouterType; use CrazyPHP\Exception\CrazyException; use CrazyPHP\Library\Array\Arrays; use CrazyPHP\Library\Cache\Cache; @@ -159,6 +160,9 @@ public function pushCollection(string $collectionPath = ""):void { # Set collection $collection = Config::get("Router"); + /* Add custom type */ + $this->addRouterType(); + /* Add Pages */ # Check router.page @@ -333,6 +337,46 @@ public function loadFromCache(string $key = ""):void { } + /** Private methods + ****************************************************** + */ + + /** + * Add Router Type + * + * Add router type define in the app + * + * @return void + */ + private function addRouterType():void { + + # Get router type config + $routerTypeCollection = Config::getValue("Router.type"); + + # Check router type + if(!empty($routerTypeCollection)) + + # Iteration router type + foreach($routerTypeCollection as $router){ + + # Check name and collectio + if( + ($router["name"] ?? false) && + ($router["class"] ?? false) && + class_exists($router["class"]) && + new $router["class"] instanceof CrazyRouterType + ){ + + # Push in router + $this->addType(strtolower($router["name"]), $router["class"]); + + } + + } + + } + + /** Public constant ****************************************************** */ diff --git a/src/Driver/Model/Config.php b/src/Driver/Model/Config.php index 1c0e545..d97ff9e 100644 --- a/src/Driver/Model/Config.php +++ b/src/Driver/Model/Config.php @@ -370,8 +370,11 @@ public static function getRouterPath(array $options = []):string { # Set route name $routeName = Context::getCurrentRoute("name"); + # Get arguments + $arguments = Query::getArguments(); + # Get reverse route - $result = Router::reverse((string)$routeName, Query::getArguments()); + $result = Router::reverse((string)$routeName, $arguments); # Get host name $hostname = isset($_SERVER['REQUEST_SCHEME']) && isset($_SERVER['HTTP_HOST']) diff --git a/src/Library/Router/Router.php b/src/Library/Router/Router.php index 196d663..3da3715 100644 --- a/src/Library/Router/Router.php +++ b/src/Library/Router/Router.php @@ -574,6 +574,11 @@ public static function reverse(string $name, ?array $arguments = null):string { # Set result $result = $routerInstance->reverse($name, $arguments ?: []); + # Fix issue from mezon + foreach ($arguments as $name => $value) { + $result = preg_replace('/\[([A-Za-z_-]*)\:' . $name . ']/', $value, $result); + } + # Return result return $result;