Controller.php & PageController.php
Controller.php defines the overall controller class. That class is extended with the PageController classes (e.g. enPageController).
In addition to the function called by the route definition, Fat-Free also calls a beforeroute and an afterroute function. The same before and after functions are used for all languages.
beforeroute – Controller.php
// HTTP route pre-processor
function beforeroute($f3) {
$f3->set('googleNoIndex',false); // Set to true when needed
$f3->set('isHomePage',false); // Set to true for home pages
$f3->set('isDetailPage',false); // Set to true for detail pages
$f3->set('pageCSS',false); // Add file name if needed
$f3->set('highlighterJavaScript',false); // Set to true for code sections
$f3->set('pageJavaScript',false); // Add file name if needed
}
What’s happening:
Set flags for things that are not required for most pages and change the flag in that page’s controller function when needed:
googleNoIndex: normally false – the only pages not indexed are the Contact and Terms of Use pages and the error pages (404 and 500).
isHomePage: normally false – flag for the home page (it has different functionality).
isDetailPage: normally false – flag for the detail pages (just need for the navigation template).
pageCSS: normally false – if CSS is required for just one or two pages, the CSS can be loaded by setting the value of pageCSS to the CSS file name. The code only supports one file, so if two CSS files are needed, the files should be combined into one file.
highlighterJavaScript: normally false – flag for loading highlight.js to highlight code syntax.
pageJavaScript: normally false – if a special JavaScript function is required for just one pages, the JavaScript can be loaded by setting the value of pageJavaScript to the template file name. I use this for loading the JavaScript for an image slider and for the special effects on the 404 page not found page and the 500 server error page. The code only supports one file, so if two JavaScript files are needed, the files should be combined into one file.
Route Function – PageController.php
function loadCommonSettings($f3) {
$f3->set('LANGUAGE','en');
$f3->set('langSubdirectory','en');
}
function RenderHome($f3) {
$this->loadCommonSettings($f3);
$f3->set('pageTitle','Learning Frameworks');
$f3->set('pageDescription','A frontend / backend setup for a simple,
multi-language website using Bootstrap 4 with the Fat-Free PHP
framework, but no database.');
$f3->set('isHomePage',true);
$f3->set('loadID',
function($array,$count) {
reset($array);
for ($x = 0; $x < $count; $x++) {
next($array);
}
return key($array);
}
);
// Set the content source
$f3->set('pageContent',$f3->get('ALIAS').'.html');
}
What’s happening:
Set page specific items in the route function (data that’s not contained in the content page):
loadCommonSettings – a local function to set values that are common to all pages for that language:
LANGUAGE – set the Fat-Free system variable to the language code for the current language. Fat-Free uses this value for selecting the language dictionary.
- langSubdirectory – This is the language subdirectory.
pageTitle – for the title tag and for social media
pageDescription – for the description meta tag and for social media
loadID – A custom function for pulling the slug value from the config.ini menus for the single page menu (only for the home page)
- The slug values could have been manually included in the template, but I wanted experience with using a custom function.
pageContent – set the file for the page content – the file name is based on the route’s alias (which is the same text string as the name of the content file)
afterroute – Controller.php
// HTTP route post-processor
function afterroute($f3) {
// Sorted the languages for the language menu
$lang_array = $f3->get('languages');
uasort($lang_array, function ($a,$b){
return ($a[2] < $b[2]) ? -1 : 1;
});
// If a regular route, use the alias, otherwise
// point the language menu to the home page (e.g. for error pages)
if ($f3->exists('ALIAS',$alias_base)) {
$alias_base = substr($alias_base,2); // the part after the lang prefix
} else {
$alias_base = '_home';
}
$language_links_array = [];
foreach($lang_array as $key=>$details) {
array_push($language_links_array,
[$key.$alias_base,$key,$details[0],$details[2]]
);
}
$f3->set('languageLinks',$language_links_array);
$pageLanguage = explode(',',$f3->get('LANGUAGE'));
$f3->set('langCode',$pageLanguage[0]); // Use langCode for the html lang tag
$lang_prefix = $f3->get('langSubdirectory'); // for local use in this function
$f3->set('mainMenu', $f3->get($lang_prefix.'HomeMenu'));
$f3->set('dropdownMenu', $f3->get($lang_prefix.'DropdownMenu'));
// Render HTML layout
echo \Template::instance()->render('layout.html','text/html');
What’s happening:
Set page specific items in the after function:
- Setup an array of the corresponding language pages for the preflang links and the language switcher:
lang_array – get the array of languages from config.ini – the default language is first and then the other languages (the order doesn’t matter)
uasort – PHP sort function – sort the array in Unicode order based on the language name (uasort preserves the key values)
alias_base – Get the base portion of the alias (e.g. en_details1 => _details1) – combine it with the other language prefixes to generate the route names to the other language versions of the same page
language_links_array – Create a new array to be passed to the template engine
- loop through the lang_array, pushing an array for each language to the language_links_array – each array contains the route name for the language versions, the language prefix (used for the language flag), and the language name.
languageLinks – pass the just created array to Fat-Free to be used for the language menu
langCode – set the value for the lang attribute in the html tag.
- Set the
mainMenu – combine the langCode with “HomeMenu” to generate the language specific menu (this does require that each menu have the same naming structure).
- Set the
dropdownMenu – combine the langCode with “DropdownMenu” to generate the language specific menu (this does require that each menu have the same naming structure).
- Echo the results from the template engine to generate the page content.