Skip to content

Commit 88ec83b

Browse files
committed
Added constant 'PROJECT_ROOT' and fixed URLs in scripts/metatags.php and sitemap.php if project is in a subdirectory
1 parent 6c8dc13 commit 88ec83b

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ e.g. all the native language names.
3636
Important is that the name of the JSON files should be the language code the file represents e.g. `en.json`, `zh-Hans.json`, etc.
3737
Inside the text you can use PHP constants defined by this framework or in the `config.php` file by simply enclosing them in `%%` e.g.
3838
`"Some text with a %%MY_CONSTANT%%"`. Variables cannot be referenced inside translations for security reasons.
39-
Longer texts like legal notices you can but into separate files inside the `translations` directory and include
40-
the content of a fiel using the following `<?php include(TRANSLATIONS.<pathInsideTranslationsDir>); ?>`
39+
Longer texts like legal notices can be put into separate files inside the `translations` directory and the file content can be included
40+
using the following `<?php include(TRANSLATIONS.<pathInsideTranslationsDir>); ?>`
4141

4242
#### `css-components/`
4343
You can put CSS files into the `css-components` directory, however they can not directly be called from an URL!
@@ -141,6 +141,7 @@ that can be used for links
141141
* `ROOT_DEPTH` Integer how deep the requested file is including the language prefix e.g. `en/users/login/index.php` --> `3`
142142
* `SUPPORTED_LANGUAGES` Array with language codes that were found in the `translations/` directory
143143
* `$_ENV` Similar to `$_GET` but instead to retrieve to environment variables defined in the `.env` file
144+
* `PROJECT_ROOT` Path from the document root of the web server `$_SERVER['DOCUMENT_ROOT']` to the root of the project (just '/' if project/framework is directly in document root and not in a subdirectory). Used in scripts/metatags.php for alternate URLs
144145

145146
### Example files that can be deleted
146147
The framework also comes with some example files that can be deleted if wanted:

index.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ function toSupportedLanguage($code){
171171

172172

173173
// Trim to actual request
174-
$base = substr($_SERVER['SCRIPT_NAME'], 0, -strlen(basename($_SERVER['SCRIPT_NAME'])));
175-
$requestWithQuery = substr($_SERVER['REQUEST_URI'], strlen($base));
174+
define('PROJECT_ROOT', substr($_SERVER['SCRIPT_NAME'], 0, -strlen(basename($_SERVER['SCRIPT_NAME']))));
175+
$requestWithQuery = substr($_SERVER['REQUEST_URI'], strlen(PROJECT_ROOT));
176176
$len = strlen($_SERVER['QUERY_STRING']);
177177
define('FULL_REQUEST', urldecode((!$len && !empty($requestWithQuery) && $requestWithQuery[-1] !== '?') ? $requestWithQuery : substr($requestWithQuery, 0, -($len+1))));
178178
$fullRequestLen = strlen(FULL_REQUEST);

scripts/metatags.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
<?php
2424

2525
foreach(SUPPORTED_LANGUAGES as $lang)
26-
echo '<link rel="alternate" href="https://'.$_SERVER['SERVER_NAME'].'/'.$lang.'/'.REQUEST.'" hreflang="'.$lang.'" />';
26+
echo '<link rel="alternate" href="https://'.$_SERVER['SERVER_NAME'].PROJECT_ROOT.$lang.'/'.REQUEST.'" hreflang="'.$lang.'" />';
2727

2828
?>

sitemap.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
$sitemapFile = 'sitemap.xml';
3333
$fmt = file_exists($sitemapFile) ? filemtime($sitemapFile) : false; // last modified time of generated sitemap file
3434
if($fmt && (time() + SITEMAP_CACHE_SECONDS) < $fmt){ echo file_get_contents($sitemapFile); exit(); }
35+
$langs = array_merge(array(''), SUPPORTED_LANGUAGES);
3536

3637
// newly generate sitemap
3738
$timeFormat = "Y-m-d\TH:i:sP";
@@ -41,8 +42,8 @@
4142
foreach(SITEMAP_URLS as $path => $info){
4243
$absPath = VIEWS.$path; if(!file_exists($absPath)) continue; // skip non existing URLs
4344
$m = filemtime($absPath); $m = ($m ? date($timeFormat, $m) : $today);
44-
foreach(SUPPORTED_LANGUAGES as $lang){
45-
$c .= "\t<url>\n\t\t<loc>https://".$domain."/".$lang."/".$path."</loc>\n\t\t<lastmod>".$m."</lastmod>\n";
45+
foreach($langs as $lang){
46+
$c .= "\t<url>\n\t\t<loc>https://".$domain.PROJECT_ROOT.$lang.(empty($lang) ? '' : '/').$path."</loc>\n\t\t<lastmod>".$m."</lastmod>\n";
4647
$c .= "\t\t<priority>".$info[0]."</priority>\n\t\t<changefreq>".$info[1]."</changefreq>\n\t</url>\n";
4748
}
4849
}

0 commit comments

Comments
 (0)