views:

54

answers:

3

I'm writing my first Codeigniter site using the MVC pattern. I'm building some controllers that load views now (haven't gotten to Models yet) but I'm noticing that my View and Controller files have the same filename (like products.php). They are in their respective folders of course. For example I have an About controller, that loads an About view, both of which are named about.php. I have a Products controller that loads a Products view which are both named products.php. Is this good practice?

From reading and studying it seems everyone names their models differently, like Products_Model.php, which makes it easy to distinguish, but I can't recall seeing anyone doing anything different with the controllers and views.

Am I setting myself up for a headache down the road when this site develops more? Thank you!

+1  A: 

The controller and model usually have the same name. This makes it easy for your main controller to call on the correct controller and model depending on a certain page name.

And in most cases it's logical to use similar names, e.g. with multiple views.

application/
  controller/
    customers.controller.php
    products.model.php
  model/
    customers.model.php
    products.model.php
  view/
    customers_list.php
    customers_add.php
    products_list.php
    products_export.php
Alec
+1  A: 

The only annoying thing I find is the tabs in your IDE are both marked 'products.php' and it's hard to tell them apart. I tend to tack '_view' onto the end of the view files now. Just do what feels right. It's not that hard to change filenames later on.

rojoca
+3  A: 

What I do when writing CodeIgniter sites is creating a folder same as the controller name in my views

And if I have a method called index(), it'll view: /views/controller_name/index.php.

In your controller you can do this: $this->load->view('controller_name/index'); so you can load the file inside the folder.

abdelm
I do this too, and this helps when a controller uses multiple views. I don't see any reason to rename my files, when I can group them up in different folders.
tpae
Exactly. Like this you will have short file names matching names of methods inside each controller. Instead of doing controller_filename.php. This is more organized.
abdelm