0

我曾在运行 wamp 2.2 (php 5.4.3) 的 Windows 上开发一个codeigniter 2.1.3应用程序。我最近将应用程序上传到运行 apache 2.2.22 和 php 5.4.6 的 ubuntu 12.04 服务器

我的模型类命名为 billView.php、categoryModel.php 等。注意大写字母。php 文件中的类的名称也是相同的。我从控制器类调用模型时给出的名称也是相同的。

但是当我在 Ubuntu 上运行我的应用程序时,我收到了这个错误

找不到您指定的型号:billview

从这一行抛出错误:

$this->load->model('billView');

(即 php 忽略了大写字母)

当我重命名模型文件时(只有模型文件名,类名保持不变),错误就会消失。

如何在不手动重命名所有文件的情况下解决此问题?

4

3 回答 3

3

文档中:

其中 Model_name 是您的班级的名称。类名的第一个字母必须大写,其余部分小写。确保您的类扩展了基础模型类。

遵循命名约定比解决它更好。

希望这可以帮助。

于 2013-07-01T13:25:54.407 回答
2

您面临的问题是 Windows 文件系统 ( NTFS) 不区分大小写,因此在 Windows 中,billview.php它们billView.php是同一个文件。

在 Linux 上,您现在可能已经猜到了,典型的文件系统(ext2/3/4、xfs、reiserfs...)是区分大小写的,因此,billview.php它们billView.php是(或可能是)不同的文件。在您的情况下,一个存在而另一个不存在。

在 CodeIgniter 自动加载器函数/方法/类/whatever 中,它正在尝试require具有您正在实例化的类的文件,因此如果您告诉它您需要模型billview,它会尝试require path/to/model/billview.php;,并且该文件不存在,所以模型未加载,然后您的应用程序失败。

当然,如果有 Amal Murali 建议的命名约定,建议遵循命名约定,但这不是这里的问题。如果代码中的所有类、文件和实例都具有相同的大小写(无论是 all_lowercase、ALL_UPPERCASE、camelCase 还是 sTuPiD_CaSe),那么一切都会奏效。

因此,请使用与创建它们相同的大小写来引用您的文件/类名,并且类名中的大写应该遵循存储它的文件名的大写。

如果您的 html 代码出于相同的原因引用不同大小写的文件(图像、css、js 文件),您将遇到同样的问题。网络服务器将寻找image.jpg但它不存在(Image.JPG例如现有文件)。

在相关说明中,php 中的变量区分大小写,但函数和类不区分大小写。尽管如此,请始终使用正确的大小写来称呼它们以避免出现问题。

于 2013-08-02T15:40:12.050 回答
0

我个人发现 CodeIgniter 中的这种约定毫无意义。我不建议破解 CodeIgniter 核心,但您可以轻松扩展 CI_Loader 类。这是我的 CI 版本 2.2.0。

<?php

class MY_Loader extends CI_Loader
{
    /**
     * Model Loader
     *
     * This function lets users load and instantiate models.
     * 
     * @param   string  the name of the class
     * @param   string  name for the model
     * @param   bool    database connection
     * @return  void
     */
    public function model($model, $name = '', $db_conn = FALSE)
    {
        if (is_array($model))
        {
            foreach ($model as $babe)
            {
                $this->model($babe);
            }
            return;
        }

        if ($model == '')
        {
            return;
        }

        $path = '';

        // Is the model in a sub-folder? If so, parse out the filename and path.
        if (($last_slash = strrpos($model, '/')) !== FALSE)
        {
            // The path is in front of the last slash
            $path = substr($model, 0, $last_slash + 1);

            // And the model name behind it
            $model = substr($model, $last_slash + 1);
        }

        if ($name == '')
        {
            $name = $model;
        }

        if (in_array($name, $this->_ci_models, TRUE))
        {
            return;
        }

        $CI =& get_instance();
        if (isset($CI->$name))
        {
            show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
        }

        //$model = strtolower($model);

        foreach ($this->_ci_model_paths as $mod_path)
        {
            if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
            {
                continue;
            }

            if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
            {
                if ($db_conn === TRUE)
                {
                    $db_conn = '';
                }

                $CI->load->database($db_conn, FALSE, TRUE);
            }

            if ( ! class_exists('CI_Model'))
            {
                load_class('Model', 'core');
            }

            require_once($mod_path.'models/'.$path.$model.'.php');

            //$model = ucfirst($model);

            $CI->$name = new $model();

            $this->_ci_models[] = $name;
            return;
        }

        // couldn't find the model
        show_error('Unable to locate the model you have specified: '.$model);
    }
}

?>

我所做的只是复制 CI_Loader::model 方法然后注释掉这两行

//$model = strtolower($model);
//$model = ucfirst($model);

您所要做的就是将上述类放在您的 application/core/ 文件夹中,它应该可以工作。

于 2014-12-27T18:21:04.297 回答