MVC pattern & best practices of folder structure
In a site where I have 2 similar forms in different pages, since some of the fields are the same, it seems logical I sh开发者_如何学JAVAould have one class that validates the fields from both forms.
My question is, where should such a class reside? In /assets
? In /includes
?
This question goes for any other files (such as 3rd party scripts) as well.
What is the best organization practice for files that do not fall in the /controller
and /model
directories?
"Assets" generally refers to static assets like images and CSS files, so that's out. If you're not using an MVC framework or if the framework you're using that doesn't have a convention for this (try googling around) I'd recommend creating a directory like /lib
, /lib/validators
, or perhaps /common
. In the end it's most important to pick something that makes sense to you (and other developers who might be working on the project). You can always move files around later if you realize a different structure might be better.
This is very complex topic to talk about, so take a look at symfony framework directory structure:
http://andreiabohner.files.wordpress.com/2007/03/cheatsheetsymfony001_enus.pdf
and try to mimic it in some way. It stores forms in /lib/form/ - you can use for example /form, if your directory structure isn't that complex. Assets are static elements like images or CSS files, as Jordan said. Includes are usually used to store config or so, it's not used in modern development at all.
I'm have a home-grown MVC where a lot of pages share common tasks and libraries, so 99% of my model's work is to call things like Form::validateForm() and return the results to the Controller rather than the model doing all the work.
This means that /foo/form can use the same Form class as /bar/form without needing to replicate any behavior.
It may not be "best practices" to have Classes handle the heavy lifting rather than models, but I do find it allows all those common tasks to be abstracted from the directory structure.
精彩评论