Codeigniter控制器和具有相同名称的模型

mus*_*usa 5 php model-view-controller codeigniter

我试着从这个评论的想法代码点火器控制器/模型名称冲突

在core/CodeIgniter.php上找到类名变量:

$class = $RTR->fetch_class(); 并改变这样:
$class = 'Controller' . $RTR->fetch_class();

现在更改控制器名称:

class ControllerUser extends CI_Controller { ...

它工作,现在我可以使用用户模型和用户控制器.但我的问题是,它有意义吗?还是问题?(抱歉,我的英文不好)

Dan*_*ing 7

我不会修改CodeIgniter的核心.升级时,您将放弃该更改.

我过去做了两件事:1.命名我的模型User_model 2.将我的控制器命名为复数,将我的模型命名为单数.

我现在做后者.这在语义上也是有意义的,因为控制器名称在URL中,因此路径看起来像app_path/users/username.此外,该模型通常模拟单个用户,因此这也是有意义的.

您还可以在此处关注此问题的社区讨论:http://codeigniter.uservoice.com/forums/40508-codeigniter-reactor/suggestions/1269847-controller-prefixes


Sta*_*arx 5

为了解决这个问题,通常大多数人都会在Model类名称中添加'_model'后缀

我认为最好在控制器中添加一个后缀,因为它们几乎从未被代码中的类名引用.

首先,我们需要扩展Router类.

创建此文件:"application/libraries/MY_Router.php"

class MY_Router extends CI_Router {
    var $suffix = '_controller';

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

    function set_class($class) {
        $this->class = $class . $this->suffix;
    }

    function controller_name() {

        if (strstr($this->class, $this->suffix)) {
            return str_replace($this->suffix, '', $this->class);
        }
        else {
            return $this->class;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

现在编辑"system/codeigniter/CodeIgniter.php"

第153行:

if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->controller_name().EXT))  
Run Code Online (Sandbox Code Playgroud)

第158行:

include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->controller_name().EXT);  
Run Code Online (Sandbox Code Playgroud)

接下来,编辑:"system/libraries/Profiler.php",第323行:

$output .= " 
<div style="color:#995300;font-weight:normal;padding:4px 0 4px 0">".$this->CI->router->controller_name()."/".$this->CI->router->fetch_method()."</div>";  
Run Code Online (Sandbox Code Playgroud)

资源