tags:

views:

66

answers:

3

When using the MVC pattern, which I'm not terribly experienced with, I find myself naming things like this:

/app/views/widget.php
/app/models/widget.php
/app/controllers/widget.php

That appeals to me because it's easy to find associated classes, and I lean towards shorter names when practical. However, when I'm looking in my IDE, I see three different files called widget.php, which is confusing. I'm tempted to add "_v", "_c", "_m" or something to each name. How do you handle this?

FWIW, I'm using CodeIgniter at the moment, and I don't know if there are any special benefits to using a particular convention, or any standard practices. Regardless, I'm intersted in the best-practices from various platforms.

+1  A: 

My view ends in phtml, so that would make Widget.phtml. My model is a Widget so that would yield Widget.php, just like that, and my controller would be WidgetController.php.

Snake
A: 

CodeIgniter (and for that matter any framework) relies on a set of 'conventions' (rules basically). One of those rules is how routing is handled. For example, the 'widget' file you have in /app/controllers/ will translate to a URL of http://yoursite.com/widget/action/ (where action are function names in your Widget class.

The normal convention is to use CamelCaseNaming for your classes and lowerCamelCase naming for methods. Each framework has a different routing engine. If you have class WidgetBlahBlah will translate to a url of /widget-blah-blah/ or /widget.blah.blah/ (depending). Action names and routing are similar.

As for naming of views, views should be named the same as your actions. They should be organized into sub directories based on your class names. Again, this is all convention. Actions in your classes look for views in specific locations named a specific name.

If you're going to use MVC I would suggest going back to the beginning and learning how to use it. MVC is designed to be rapid development by understanding a set of conventions and leveraging them. Maybe start here: http://codeigniter.com/user_guide/toc.html

Matt S
sprugman
What IDE are you using? I realize that there seems like a lot of duplicates but that's part of the MVC convention I suppose you could say. Also, like Snake mentions below you MAY need to call your views `.phtml` as that may be what CodeIgniter looks for.
Matt S
I'm using NetBeans. (This isn't really the place, but in my naive opinion, MVC sometimes seems to conflict with DRY.)
sprugman
That depends. MVC architecture means you encapsulate all your logic relating to tables / data from tables in models. Logic for putting together your page views in controllers, and rendering in views. If you have a bit of a view that gets used in multiple places you make a partial or a helper. If you have logic that is generic and used in multiple places you can pull it to a library.
Matt S
A: 

I personally think that having everything named widget.php gets confusing even if the files are in separate files. I tend to append either Model, View, or Controller to the end of the files names in addition to having the files divided into respective folders. While it is more verbose it is much more expressive and easier for newcomers to your code base to follow your code. So my widget (in Java which is what I most frequently use for mvc) would be named as follows:

/app/widget/view/WidgetView.jsp
/app/widget/model/WidgetModel.java
/app/widget/controller/WidgetController.java
/app/coolwidget/view/CoolWidget.java
...

Plus when I'm in an IDE or editor I tend to look at the file name and not the full path to the file when editing. So if I'm editing the Model, View, and Controller for my widget, I don't want to be examining the file paths to figure out which one I'm working on.

tkeE2036