• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
controller and model with same name

#1
[eluser]bhbutter123[/eluser]
I have a controller and model with the same class name (photos) and when I try to use the photos model in the photos controller I get a cannot redeclare class photos error(as I should). Is there a way I can have a model and controller with the same name because it would get annoying to have to keep typing out _model whenever I include the model.

#2
[eluser]richthegeek[/eluser]
No - this isn't an issue with CI but with PHP itself - you cannot have two classes instantiated with the same name.

#3
[eluser]Ben Edmunds[/eluser]
It's a namespace issue. The short answer is what richthegeek said, they need to be named differently.

#4
[eluser]CroNiX[/eluser]
I name all my models: controllerName_model. It avoids your problem and I think it makes for more semantic code.

Like:
$this->photo_model->get_all();
makes it really easy for you to understand at a glance what is going on. You're getting info from your photo model.

#5
[eluser]jgaujardtj[/eluser]
Hello,

What do you think about :
- named all controller as plural (eg: Users)
- named all model as singular (eg: User)

Joël

#6
[eluser]ivantcholakov[/eluser]
With an adaptation for CodeIgniter 3 I use this trick:

Hack 2. Prevent Model-Controller Name Collision, http://net.tutsplus.com/tutorials/php/6-...e-masters/

#7
[eluser]jgaujardtj[/eluser]
Thanks for your reply ivantcholakov,

So you prefer to modify the core and suffix controller instead of model.
Sorry I'm not a fan of this solution.
Thanks anyway

#8
[eluser]ivantcholakov[/eluser]
I've modified the hack for maintaining backward compatibility, so for me '_controller' suffix is not mandatory. But I prefer for new projects always to use the '_controller' suffix (for controllers) for newly created code. As the article mentioned you will use model names much often, so it is preferable model names to be cleaner and expressive. I hate seeing things like:

$this->users_model->...
$this->users_m->...

They uglify code in unacceptable for me level.

I think, the solution is nice, it merits to be implemented (with adaptation and BC) within CodeIgniter 3.

Of course, for your particular case it is your project and your own decision.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.