【问题标题】:CodeIgniter: Librariy class not loading with this codeCodeIgniter:库类未使用此代码加载
【发布时间】:2013-11-20 10:12:00
【问题描述】:

我是 CodeIgniter 的新手,并按照 Nettuts 高级教程学习如何在 codeigniter 中构建 CMS。

首先,我的 applicationsystem 目录位于 public_html 之外的不同路径上,并在 config.php 文件中设置路径。

我创建了两个文件 Frontend_Controller.php 和 Admin_Controller.php 并在这些文件中创建了如下类。

application/My_Controller.php

class MY_Controller extends CI_Controller
{

    public $data = array();

    function __construct()
    {
        parent::__construct();

        $this->data['errors'] = array();
        $this->data['site_name'] = config_item('site_name');
    }
}

库/Frontend_Controller.php

class Frontend_Controller extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
    }
}

库/Admin_Controller.php

class Admin_Controller extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
    }
}

现在,当我尝试通过将 __autoload() 函数写入 config.php 文件来自动加载上述类时,但它不会加载文件/类

config.php

function __autoload($classname)
{
    if(strpos($classname, 'CI_' !== 0))
    {
        $file = APPPATH . 'libraries/' . $classname . '.php';

        if(file_exists($file) && is_file($file))
        {
            @include_once($file);
        }
    }
}

谁能帮我理解和解决这个问题...非常感谢

【问题讨论】:

  • 为什么不使用 CI 的自动加载?为什么要在配置中加载类??
  • 哦!我不太了解它。你能教我怎么做吗?
  • 您阅读文档了吗?挺好的,相信会回答你的问题的。作为个人建议,您应该在使用之前阅读框架/库文档...
  • 是的,我已经阅读了文档并需要在 autoload.php 中定义,但我仍然很困惑如何编写类名以及在哪里编写?因为我在库中有控制器类也有大写的文本。

标签: php codeigniter


【解决方案1】:

您可以尝试在application/config/autoload.php 文件中使用 codeigniter 自动加载 像这样:

`$autoload['libraries'] = array('database', 'session');`

详情请看这里:http://ellislab.com/codeigniter%20/user-guide/general/autoloader.html

你也可以试试这个:

下面的 sn-ps 查看小写文件名的以下位置:core/libraries/models/

function __autoload($class) 
{
  $path = array('core', 'libraries', 'models');

  if(strpos($class, 'CI_') !== 0) {
    foreach($path as $dir) {
      if (file_exists(APPPATH.$dir.'/'.strtolower($class).'.php'))
        include_once(APPPATH.$dir.'/'.strtolower($class).'.php');
    }
  }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
  • 1970-01-01
  • 2018-02-26
  • 2016-06-11
  • 1970-01-01
  • 1970-01-01
  • 2020-06-08
相关资源
最近更新 更多