diff --git a/src/flextype/helpers/helpers.php b/src/flextype/helpers/helpers.php index 4afb26b7..956852ed 100644 --- a/src/flextype/helpers/helpers.php +++ b/src/flextype/helpers/helpers.php @@ -7,4 +7,5 @@ require_once ROOT_DIR . '/src/flextype/helpers/arrays.php'; require_once ROOT_DIR . '/src/flextype/helpers/finder.php'; require_once ROOT_DIR . '/src/flextype/helpers/image.php'; require_once ROOT_DIR . '/src/flextype/helpers/upload.php'; -require_once ROOT_DIR . '/src/flextype/helpers/tokens.php'; \ No newline at end of file +require_once ROOT_DIR . '/src/flextype/helpers/tokens.php'; +require_once ROOT_DIR . '/src/flextype/helpers/urls.php'; \ No newline at end of file diff --git a/src/flextype/helpers/urls.php b/src/flextype/helpers/urls.php new file mode 100644 index 00000000..febd1b6b --- /dev/null +++ b/src/flextype/helpers/urls.php @@ -0,0 +1,128 @@ + $data Route placeholders. + * @param array $queryParams Query parameters. + * + * @return string Url for a named route. + */ + function urlFor(string $routeName, array $data = [], array $queryParams = []): string + { + return app()->getRouteCollector()->getRouteParser()->urlFor($routeName, $data, $queryParams); + } +} + +if (! function_exists('fullUrlFor')) { + /** + * Get the full url for a named route. + * + * @param ServerRequestInterface $request Servert request interface. + * @param string $routeName Route name. + * @param array $data Route placeholders. + * @param array $queryParams Query parameters. + * + * @return string Full url for a named route. + */ + function fullUrlFor(ServerRequestInterface $request, string $routeName, array $data = [], array $queryParams = []): string + { + return app()->getRouteCollector()->getRouteParser()->fullUrlFor($request->getUri(), $routeName, $data, $queryParams); + } +} + +if (! function_exists('isCurrentUrl')) { + /** + * Determine is current url equal to route name. + * + * @param ServerRequestInterface $request Servert request interface. + * @param string $routeName Route name. + * @param array $data Route placeholders. + * + * @return bool + */ + function isCurrentUrl(ServerRequestInterface $request, string $routeName, array $data = []): bool + { + $currentUrl = getBasePath() . $request->getUri()->getPath(); + $result = app()->getRouteCollector()->getRouteParser()->urlFor($routeName, $data); + + return $result === $currentUrl; + } +} + +if (! function_exists('getCurrentUrl')) { + /** + * Get current path on given Uri. + * + * @param ServerRequestInterface $request Servert request interface. + * @param bool $withQueryString Get query string for current path. + * + * @return string + */ + function getCurrentUrl(ServerRequestInterface $request, bool $withQueryString = false): string + { + $currentUrl = getBasePath() . $request->getUri()->getPath(); + $query = $request->getUri()->getQuery(); + + if ($withQueryString && !empty($query)) { + $currentUrl .= '?'.$query; + } + + return $currentUrl; + } +} + +if (! function_exists('getBasePath')) { + /** + * Get the base path + * + * @return string Base Path. + */ + function getBasePath(): string + { + return app()->getBasePath(); + } +} + +if (! function_exists('setBasePath')) { + /** + * Set the base path + * + * @param string $basePath Base path. + * + * @return void + */ + function setBasePath(string $basePath) + { + app()->setBasePath($basePath); + } +} + +if (! function_exists('redirect')) { + /** + * Redirect + * + * @param string $routeName Route name + * @param array $data Route placeholders + * @param array $queryParams Query parameters + * + * @return Response + */ + function redirect(string $routeName, array $data = [], array $queryParams = []): Response + { + $response = new Response(); + $response->withHeader('Location', urlFor($routeName, $data, $queryParams)); + + return $response; + } +} \ No newline at end of file