Codeigniter类和文件名区分大小写在Linux(centos)

时间:2015-06-30 18:00:31

标签: php linux codeigniter

我遇到了一个区分大小写的问题,我无法绕过它。

这就是我的文件结构。我只是输入了我正在使用的目录,但实际上我正在使用CI3的完整安装。

/application
    ....
    /controllers/
        application_controller.php
    /core/
        MY_Controller.php
        Public_controller.php
    ....
    /models/
        Application_model.php
    ....

以下是类定义语法的含义:

/application/core/My_Controller.php

class MY_Controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }
}

/application/core/Public_Controller.php

class Public_Controller extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    // Application logic here...
}

/application/controllers/application_controller.php

class Application_controller extends Public_Controller
{

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

    // Application logic here...
}

阅读文档,我看到我应该为我的类命名:

Foo_Controller.php

然后我总是(认为)类定义应该与文件名匹配。所以:

class Foo_Controller extends Bar_Controller {
    ....
}

然后我得到500错误,或者我没有错误,还有一个白页。 当我在本地工作(mac)时,一切都很完美。截至目前(使用上面的语法)我至少得到了默认的codeigniter 404页面。使用时

error_log(__FILE__); 

在每个班级的顶部,我所要到的只是My_Controller.php

感谢您的任何建议!

3 个答案:

答案 0 :(得分:7)

Codeigniter change log

  

更改了文件范围约定(类文件名现在必须 Ucfirst 其他所有小写)。

所以你的控制器和文件名应该是

  • My_controller(仅M大写小写休息小写)
  • Public_controller
  • Application_controller
  • Foo_controller

我自己不喜欢新的命名惯例CI-2在这种情况下更好。

注意:

控制器名称Public_Controller(C大写)和文件名Public_controller.php可能有效,但我更喜欢保持两个名称相同,因此控制器名称应为Public_controller

答案 1 :(得分:2)

正如@ jagad89所说,application_controller.php的文件名应为as the codeigniter upgrade guide specifies: “......必须以类似Ucfirst的方式命名,换句话说 - 必须以大写字母开头。”所以Application_controller.php

意味着所有控制器,模型,库和驱动程序( NOT HELPERS 都以这种方式命名,以便能够在codeigniter中使用。

为了统一,你的类定义应该如你所说的那样匹配文件名。

您应该在日志中看到500个错误,至少在linux上,/var/log/apache2/error.log这应该有助于您的调试过程。

答案 2 :(得分:0)

我的解决方案 在/ application / core / i中创建一个MY_Loader.php(MY_L的大写)
我创建了一个类MY_Loader,其中包含原始函数“model”的副本
我评论“$ model = ucfirst($ model);”这一行
/application/core/MY_Loader.php的代码:

class MY_Loader extends CI_Loader
{
    /*this a copy of function model of the class CI_Loader from /system/core/Moader.php */
    public function model($model, $name = '', $db_conn = FALSE)
    {
        if (empty($model))
        {
            return $this;
        }
        elseif (is_array($model))
        {
            foreach ($model as $key => $value)
            {
                is_int($key) ? $this->model($value, '', $db_conn) : $this->model($key, $value, $db_conn);
            }

            return $this;
        }

        $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);

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

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

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

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

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

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

        // Note: All of the code under this condition used to be just:
        //
        //       load_class('Model', 'core');
        //
        //       However, load_class() instantiates classes
        //       to cache them for later use and that prevents
        //       MY_Model from being an abstract class and is
        //       sub-optimal otherwise anyway.
        if ( ! class_exists('CI_Model', FALSE))
        {
            $app_path = APPPATH.'core'.DIRECTORY_SEPARATOR;
            if (file_exists($app_path.'Model.php'))
            {
                require_once($app_path.'Model.php');
                if ( ! class_exists('CI_Model', FALSE))
                {
                    throw new RuntimeException($app_path."Model.php exists, but doesn't declare class CI_Model");
                }
            }
            elseif ( ! class_exists('CI_Model', FALSE))
            {
                require_once(BASEPATH.'core'.DIRECTORY_SEPARATOR.'Model.php');
            }

            $class = config_item('subclass_prefix').'Model';
            if (file_exists($app_path.$class.'.php'))
            {
                require_once($app_path.$class.'.php');
                if ( ! class_exists($class, FALSE))
                {
                    throw new RuntimeException($app_path.$class.".php exists, but doesn't declare class ".$class);
                }
            }
        }

        //~ $model = ucfirst($model);/*this the line that i comment*/
        if ( ! class_exists($model, FALSE))
        {
            foreach ($this->_ci_model_paths as $mod_path)
            {
                if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
                {
                    continue;
                }

                require_once($mod_path.'models/'.$path.$model.'.php');
                if ( ! class_exists($model, FALSE))
                {
                    throw new RuntimeException($mod_path."models/".$path.$model.".php exists, but doesn't declare class ".$model);
                }

                break;
            }

            if ( ! class_exists($model, FALSE))
            {
                throw new RuntimeException('Unable to locate the model you have specified: '.$model);
            }
        }
        elseif ( ! is_subclass_of($model, 'CI_Model'))
        {
            throw new RuntimeException("Class ".$model." already exists and doesn't extend CI_Model");
        }

        $this->_ci_models[] = $name;
        $CI->$name = new $model();
        return $this;
    }   
}