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)

2
  • This is why you should try to find a framework which lets you work with PHP5.3 namespaces. 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. Commented Mar 24, 2012 at 9: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. Commented Mar 26, 2013 at 12:15

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 phpfour.com/blog/2009/09/… what do you think?
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.
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

6 Comments

@musa, that was with the hmvc solution, Check my edit and update
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, You are right, I didn't modified the contents of the source page. But you have a good point, I updated it. Thanks
@Starx What do you think about this solution: phpfour.com/blog/2009/09/… just one MY_Router class, no core editing.
@musa, I like it, but the hack above does less traversing among the request.
|
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

1 Comment

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
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.