我正在尝试创建一个构建和显示导航菜单的功能,同时尽可能地保持 MVC(虽然我是新手,所以我不完全理解它)。
我的脚本在没有提供错误消息的情况下死亡。让我们调查一下!
在我看来,我调用了一个构建菜单内容的函数,并发送菜单中应该存在的页面名称:
// 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>
现在的想法是遍历这些参数,并为每个参数构建一个列表项,其中包含文件名(也是 URL slug)和适当语言的显示名称。
// 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";
}
}
?>
当我看到它时,我觉得它有点道理,但同时提出了诸如“$this
给定上下文中的内容是什么?”之类的问题</p>
如果我更改 $this->lang->line($token)
为 just $token
,脚本就会运行(尽管我没有获得多语言功能)。
我有我需要的语言文件……</p>
// 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";
?>
…在这里你可以看到我正在将我的语言文件加载到我的控制器中(这几乎完全反映了 CI 文档中的页面控制器):
<?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);
}
}
?>