5

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)

Community
  • 1
  • 1
musa
  • 1,457
  • 18
  • 37
  • This is why you should try to find a framework which lets you work with PHP5.3 [namespaces](http://php.net/manual/en/language.namespaces.php). As much as i hate that framework, FuelPHP might be the logical step for you. Its quite new CI fork. Thats, of course, if you have any control over which framework is used or you are at the stage in which it is possible. If not .. just keep it for consideration when you pick up next project. – tereško Mar 24 '12 at 09:23
  • Don't know where you get the idea from the FuelPHP is a fork of CI. It isn't, is was built from scratch, has nothing in common with CI, and is not compatible. – WanWizard Mar 26 '13 at 12:15

4 Answers4

7

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

Dan Bowling
  • 1,205
  • 1
  • 15
  • 41
  • I'm using datamapper, so I don't want to make User_model. but yes, models User, controllers Users can be. also I found this http://phpfour.com/blog/2009/09/codeigniter-controller-naming-convention-modified/ what do you think? – musa Mar 24 '12 at 02:35
  • as far as #2 goes, thus far makes sense, and I think this is a bit more SEO friendly, but would like to get more references on that. – James John McGuire 'Jahmic' Sep 20 '13 at 08:43
5

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

Starx
  • 77,474
  • 47
  • 185
  • 261
  • @musa, that was with the hmvc solution, Check my edit and update – Starx Mar 24 '12 at 03:19
  • shouldn't it be `parent::__construct()` ? Since PHP5 there has been a change in how you perform the construction ... then again CI is still semi-php4 frameworks. – tereško Mar 24 '12 at 09:19
  • @tereško, You are right, I didn't modified the contents of the source page. But you have a good point, I updated it. Thanks – Starx Mar 24 '12 at 10:32
  • @Starx What do you think about this solution: http://phpfour.com/blog/2009/09/codeigniter-controller-naming-convention-modified/ just one MY_Router class, no core editing. – musa Mar 24 '12 at 13:24
  • @musa, I like it, but the hack above does less traversing among the request. – Starx Mar 24 '12 at 13:31
  • This trick is not applicable to Codeigniter newer versions. You have to do some another tricks ! – Hatem Oct 19 '13 at 13:14
1

Yup this makes sence. Codeigniter is a strict Object Oriented framework. This means that when you have 2 or more objects with the same name, the name of the object is not unique enough.

In your case a model is a data processor and a controller the class that connects your view to the model. So logically you must name it something like this:

Your model will be: "Model_User", "Db_User" or "UserList"

Your controller will be: "User", Controller_User or UserController

In case of the model I suggest you use Db_User (short but clear names) and in the controllers case I suggest you use "User" (short and the class which defines the user when its combined with the model and view for that user)

I never have problems using my Codeigniter systems like this

Chris Visser
  • 1,607
  • 12
  • 24
  • I'm not using plural because i'm speaking of a list or a table. Users for example are strictly spoken properties of a list – Chris Visser Mar 24 '12 at 17:32
1

Why dont you just make your controllers singular and your models/db tables plural ?

Philip
  • 4,592
  • 2
  • 20
  • 28