Codeigniter Class and filename case sensitive on L

2019-01-20 03:42发布

问题:

I am running into a case-sensitive problem that I'm not able to wrap my head around it appears.

This is what my file structure looks like. I am only entering the directories that I am working with, but I am in fact using a full-install of CI3.

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

Here is what the class definition syntax looks like:

/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...
}

Reading the docs, I see that I should name my classes something like:

Foo_Controller.php

Then i've always (thought) that the class definition should match the file name. So:

class Foo_Controller extends Bar_Controller {
    ....
}

Then I either get a 500 error, or I get no errors, and a white page. When I work locally (mac) everything works perfectly. As of right now (using the syntax above) I am at least getting the default codeigniter 404 page. When use

error_log(__FILE__); 

at the top of each class, all I am getting to is My_Controller.php

Thank you for any suggestions!

回答1:

Codeigniter change log says

Changed filenaming convention (class file names now must be Ucfirst and everything else in lowercase).

So you controllers and files name should be

  • My_controller (only M upper case rest lower case)
  • Public_controller
  • Application_controller
  • Foo_controller

I myself don't like new naming convention CI-2 was better in this case.

Note:

Controller name Public_Controller(C uppercase) and file name Public_controller.php may work but I prefer to keep both name same so controller name should be Public_controller.



回答2:

As @jagad89 said, your filename for application_controller.php should be as the codeigniter upgrade guide specifies: "... must be named in a Ucfirst-like manner or in other words - they must start with a capital letter." so Application_controller.php

Meaning all of your Controllers, Models, Libraries, and Drivers (NOT HELPERS) have to be named in this manner to be able to be used inside of codeigniter.

For sake of unity, your class definition should match the filename as you said.

You should see the 500 errors in your log, at least on linux, at /var/log/apache2/error.log which should help with your debugging process.



回答3:

my solution in the /application/core/ i create a MY_Loader.php (uppercase for MY_L)
I create a class MY_Loader with a copy of original function "model"
I comment the line "$model = ucfirst($model);"

The code of /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;
    }   
}