- Fixed the subdirectory loading issue.
Changed: [app/View.php](/Users/tyemeclifford/Documents/GH/blog/app/View.php): generated URLs now honor a base path like /blog. [app/Config.php](/Users/tyemeclifford/Documents/GH/blog/app/Config.php): added base_path support and derives it from base_url or SCRIPT_NAME. [app/App.php](/Users/tyemeclifford/Documents/GH/blog/app/App.php): route parsing strips the base path, and PHP can safely serve theme/upload assets for virtual subdirectory previews. [.htaccess](/Users/tyemeclifford/Documents/GH/blog/.htaccess): removed hardcoded RewriteBase /. [themes/neon/layout.php](/Users/tyemeclifford/Documents/GH/blog/themes/neon/layout.php): root-relative social links like /feed.xml now become /blog/feed.xml. [README.md](/Users/tyemeclifford/Documents/GH/blog/README.md): documented subdirectory setup.
This commit is contained in:
@@ -81,6 +81,12 @@ final class Config
|
||||
{
|
||||
$configured = trim((string) $this->get('base_url', ''));
|
||||
if ($configured !== '') {
|
||||
$parts = parse_url($configured);
|
||||
if (is_array($parts) && isset($parts['scheme'], $parts['host'])) {
|
||||
$port = isset($parts['port']) ? ':' . $parts['port'] : '';
|
||||
return $parts['scheme'] . '://' . $parts['host'] . $port;
|
||||
}
|
||||
|
||||
return rtrim($configured, '/');
|
||||
}
|
||||
|
||||
@@ -96,6 +102,34 @@ final class Config
|
||||
return $scheme . '://' . $host;
|
||||
}
|
||||
|
||||
public function basePath(): string
|
||||
{
|
||||
$configured = trim((string) $this->get('base_path', ''));
|
||||
if ($configured !== '') {
|
||||
return self::normalizeBasePath($configured);
|
||||
}
|
||||
|
||||
$baseUrl = trim((string) $this->get('base_url', ''));
|
||||
if ($baseUrl !== '') {
|
||||
$path = parse_url($baseUrl, PHP_URL_PATH);
|
||||
if (is_string($path) && $path !== '') {
|
||||
return self::normalizeBasePath($path);
|
||||
}
|
||||
}
|
||||
|
||||
if (PHP_SAPI === 'cli') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$scriptName = str_replace('\\', '/', (string) ($_SERVER['SCRIPT_NAME'] ?? ''));
|
||||
$scriptDir = dirname($scriptName);
|
||||
if ($scriptDir === '.' || $scriptDir === '/') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return self::normalizeBasePath($scriptDir);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private static function defaults(): array
|
||||
{
|
||||
@@ -108,6 +142,7 @@ final class Config
|
||||
'language' => 'en',
|
||||
],
|
||||
'base_url' => '',
|
||||
'base_path' => '',
|
||||
'timezone' => 'UTC',
|
||||
'theme' => 'neon',
|
||||
'posts_per_page' => 6,
|
||||
@@ -124,4 +159,10 @@ final class Config
|
||||
'social' => [],
|
||||
];
|
||||
}
|
||||
|
||||
private static function normalizeBasePath(string $path): string
|
||||
{
|
||||
$path = '/' . trim($path, '/');
|
||||
return $path === '/' ? '' : $path;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user