What do you name your files when using MVC?
When using the MVC pattern, which 开发者_如何学GoI'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.
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
.
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.
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
I generally just leave them named without any special convention, and distinguish between the different files by looking at:
- folder names
- file contents
With syntax highlighting, a view file containing mostly html is very easy to distinguish from a controller or model. As for telling the difference between models and controllers: I don't generally name models and controllers with the same names, so there isn't a problem there for me.
精彩评论