Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter controller and model with same name collison

I'm try something from this comment idea Code Igniter Controller/Model name conflicts

find class name variable on core/CodeIgniter.php :

$class = $RTR->fetch_class(); and change like that:
$class = 'Controller' . $RTR->fetch_class();

now change controller name:

class ControllerUser extends CI_Controller { ...

It works, now I can use User model and User controller. But my question is, Does it make sense? or Does the problem? (sorry my bad English)

like image 969
musa Avatar asked Mar 24 '12 01:03

musa


2 Answers

I would not modify the core of CodeIgniter. When you upgrade, you'll loose that change.

I've done two things in the past: 1. Named my models User_model 2. Named my controllers as plural, and my models as singular.

I do the latter now. This semantically makes sense too, because the controller name is in the URL, so paths look like app_path/users/username. Also, the model usually models a single user, so that makes sense too.

You can also follow some discussion from the community on this question here: http://codeigniter.uservoice.com/forums/40508-codeigniter-reactor/suggestions/1269847-controller-prefixes

like image 118
Dan Bowling Avatar answered Sep 28 '22 16:09

Dan Bowling


To get around this issue, normally most people add the ‘_model’ suffix to the Model class names

I think it is better to add a suffix to the Controllers instead, since they are almost never referenced by their class names in your code.

First we need to extend the Router class.

Create this file: “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;
        }

    }
}

Now edit “system/codeigniter/CodeIgniter.php”

line 153:

if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->controller_name().EXT))  

line 158:

include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->controller_name().EXT);  

Next, edit: “system/libraries/Profiler.php”, line 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>";  

Source

like image 20
Starx Avatar answered Sep 28 '22 16:09

Starx