I'm trying to create a function that builds and displays a navigation menu, whilst keeping to MVC as much as possible (though I'm new to it, so I don't understand it completely).
My script dies without providing an error message. Let's investigate!
In my view, I call a function that builds the menu's contents, and send in the names of the pages that should exist in the menu:
// application/views/templates/header.php
<ul class="navigation">
<?php
// Send in the English name, which also becomes the slug.
// Function should return the name in the appropriate language,
// plus the slug in English.
$args = ['home','compete','gallery','finalists','about'];
build_navigation($args);
?>
</ul>
The idea now is to loop through those arguments, and build a list-item for each argument containing the file name — which is also the URL slug — and the display name in the appropriate language.
// application/helpers/navigation_helper.php
// This is loaded in autoload.php — confirmed working
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('build_navigation')) {
function build_navigation($args) {
foreach ($args as $token)
echo "<li><a href=\"{$token}\">{$this->lang->line($token)}</a></li>\n";
}
}
?>
When I look at that, it sort of makes sense in my head, but at the same time raises questions like “what is $this
in the given context?”
If I change $this->lang->line($token)
to just $token
, the script runs (though I don't get my multi-language functionality).
I have the language files I need…
// application/language/english/en_lang.php
<?php
$lang['home'] = "Home";
$lang['compete'] = "Compete";
$lang['gallery'] = "Gallery";
$lang['finalists'] = "Finalists";
$lang['about'] = "About";
?>
// application/language/swedish/sv_lang.php
<?php
$lang['home'] = "Hem";
$lang['compete'] = "Tävla";
$lang['gallery'] = "Galleri";
$lang['finalists'] = "Finalister";
$lang['about'] = "Info";
?>
…And here you can see that I'm loading my language files in my controller (which almost exactly mirrors the pages controller in the CI docs):
<?php
/**
* Pages
*
* Class for building static pages.
*
*/
class Pages extends CI_Controller {
public function view ($page = 'home') {
if (!file_exists('application/views/pages/'.$page.'.php'))
show_404();
$data['title'] = ucfirst($page); // Capitalise first letter of title
$this->lang->load('en','english');
$this->lang->load('sv','swedish');
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
?>