我的代码点火器应用程序中定义了一个钩子,用于根据加载的控制器加载语言。
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files. Please see the user guide for info:
|
| https://codeigniter.com/user_guide/general/hooks.html
|
*/
$hook['post_controller_constructor'] = array(
'class' => 'LanguageLoader',
'function' => 'initialize',
'filename' => 'LanguageLoader.php',
'filepath' => 'hooks'
);
这就是LanguageLoader.php的样子
<?php
class LanguageLoader
{
function initialize() {
$ci =& get_instance();
$class = $ci->router->class;
$class = strtolower($class);
$ci->load->helper('language');
$ci->load->library('session');
if(!$ci->session->has_userdata('language')){
$ci->session->set_userdata('language','english');
$ci->session->set_userdata('language_remaining','spanish');
}
$ci->lang->load($class,$ci->session->userdata('language'));
$ci->lang->load('dashboard',$ci->session->userdata('language'));
}
}
这个问题是,如果我访问不存在的控制器的URL,它只会抛出找不到语言文件而不是抛出404错误。如果我将钩子更改为预控制器,我会得到404错误,但是当使用存在的控制器时,语言文件不会自动加载。
解决此问题的首选方法是什么?检查控制器是否存在于钩子中?或者我唯一希望手动加载我的语言文件?
由于
答案 0 :(得分:1)
<?php
class LanguageLoader
{
function initialize() {
$ci =& get_instance();
$class = $ci->router->class;
$class = strtolower($class);
$ci->load->helper('language');
$ci->load->library('session');
if(!$ci->session->has_userdata('language')){
$ci->session->set_userdata('language','english');
$ci->session->set_userdata('language_remaining','spanish');
}
$path = '_botadmin/language/'.$ci->session->userdata('language').'/'.$class.'_lang.php';
if(file_exists($path)){
$ci->lang->load($class,$ci->session->userdata('language'));
$ci->lang->load('dashboard',$ci->session->userdata('language'));
}
}
}