Skip to content

Enhancements URL and Pages #3674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
40 changes: 36 additions & 4 deletions core/classes/Core/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function add(string $module, string $url, string $file, string $name = ''
{
$this->_pages[$url] = [
'module' => $module,
'url' => $url,
'file' => $file,
'name' => $name,
'widgets' => $widgets,
Expand All @@ -66,6 +67,7 @@ public function addCustom(string $url, string $name, bool $widgets = false): voi
{
$this->_pages[$url] = [
'module' => 'Core',
'url' => $url,
'file' => 'pages/custom.php',
'name' => $name,
'widgets' => $widgets,
Expand Down Expand Up @@ -126,8 +128,8 @@ public function getSitemapMethods(): array
/**
* Get page by ID.
*
* @param int $page_id ID of page to find.
* @return array Page information.
* @param int $page_id ID of page to find.
* @return array|null Page information.
*/
public function getPageById(int $page_id): ?array
{
Expand All @@ -145,8 +147,8 @@ public function getPageById(int $page_id): ?array
/**
* Get page by URL.
*
* @param string $url URL of page to find.
* @return array Page information.
* @param string $url URL of page to find.
* @return array|null Page information.
*/
public function getPageByURL(string $url): ?array
{
Expand All @@ -161,6 +163,36 @@ public function getPageByURL(string $url): ?array
return null;
}

/**
* Get page by name.
*
* @param string $name
* @return array|null Page information.
*/
public function getPageByName(string $name): ?array
{
foreach ($this->_pages as $key => $page) {
if ($page['name'] == $name) {
$page['key'] = $key;

return $page;
}
}

return null;
}

/**
* Returns all pages belonging to a particular module.
*
* @param string $module
* @return array
*/
public function getPagesByModule(string $module): array
{
return array_filter($this->_pages, fn ($page) => $page['module'] === $module);
}

/**
* Get the page details the user currently viewing.
*
Expand Down
32 changes: 32 additions & 0 deletions core/classes/Core/URL.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,36 @@ public static function urlSafe(string $text): string

return urlencode(strtolower(str_replace(' ', '-', $text)));
}

/**
* Breaks any URI into segments (without a leading/closing “/”).
*
* @param string|null $uri If null — takes $_SERVER['REQUEST_URI']
* @return string[] Array of segments
*/
public static function getSegments(?string $uri = null): array
{
if ($uri === null) {
$uri = $_SERVER['REQUEST_URI'] ?? '/';
}
$path = parse_url($uri, PHP_URL_PATH) ?: '';
// We remove the presenter and closing slash, break by "/"
$parts = array_filter(explode('/', trim($path, '/')), fn ($s) => $s !== '');

return array_values($parts);
}

/**
* Returns a specific index segment (0-Based) or $ default if absent.
*
* @param int $index Segment index (0 - first)
* @param string|null $default The default value
* @return string|null
*/
public static function segment(int $index, ?string $default = null): ?string
{
$segments = self::getSegments();

return $segments[$index] ?? $default;
}
}