doujian0265 2017-02-16 04:08
浏览 100
已采纳

Codeigniter MY_loader未加载

I am trying to create a header / body / footer template in CodeIgniter similar to that described at: Header and footer in CodeIgniter

My code stored at \application\core\MY_loader.php:

<?php
class MY_Loader extends CI_Loader {

    public function load_template($template_name, $vars = array(), $return = FALSE)
    {
        if($return):
            $content  = $this -> view('templates/header', $vars, $return);
            $content .= $this -> view($template_name, $vars, $return);
            $content .= $this -> view('templates/footer', $vars, $return);

            return $content;

        else:
            $this -> view('templates/header', $vars);
            $this -> view($template_name, $vars);
            $this -> view('templates/footer', $vars);

        endif;
    }
}
?>

My controller code stored at application\controllers\managers.php:

class Managers extends CI_Controller {
    function login()
    {
        $this -> load -> load_template('managers/login');
    }
}

When I browse to BASE_URL/managers/login, I get this error:

Call to undefined method CI_Loader::load_template()

My interpretation of this is that the system is not extending CI_Loader with MY_Loader, but is instead disregarding MY_Loader entirely. This setup was working on my local install of the site when I was running it under XAMPP, but it stopped working after I ported the site to a web host. I don't remember changing the CI configuration (although I might have), nor do I know if this is due to a configuration issue at the new host.

I am looking for any guidance as to what might be preventing MY_loader from extending CI_loader. I haven't been able to find any similar reports; all the other issues I've found related to MY_loader assume that the override is already working.

  • 写回答

2条回答 默认 最新

  • dongpeng7744 2017-02-17 15:00
    关注

    The file has to be called "MY_Loader.php" - it is case-sensitive and NOT the same as "MY_loader.php".

    Contrary to the only other answer at this time, "My_loader.php" will NOT work either, as the subclass_prefix is applied separately from the library name.

    The simplest way to exemplify it is this:

    $libraryName = 'loader';
    
    $className   = ucfirst(strtolower($libraryName));
    $className   = config_item('subclass_prefix').$className;
    
    $fileName    = $className.'.php';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)