I have been doing a lot of research on MVC and file structure. Mainly I've been looking at how to start a new layout. I have downloaded a few open source applications to take a look at file structure and how files are developed.
In the first application it was set up to use the standard way (at least the way it seems to me) of putting all the controllers, models and views each in their respective folders. This is the way that all the books say to do it.
In the second application, all folders are in a modules_core or modules folder where each controller (at least what I would assume to be controllers) are in a folder in there that contain three folders: controller, model, view.
Which of the two versions is accepted as standard and common practice? Are the two applications different because of versions of Codeigniter?
The standard of Code Igniter is to use those three folders:
Controllers
Models
Views
You can also create sub folders to better separate your files.
Searching a bit, I found that MyClientBase use something called codeigniter-modular-extensions-hmvc that is like a extension for CI.
Modular Extensions makes the CodeIgniter PHP framework modular.
Modules are groups of independent components, typically model,
controller and view, arranged in an application modules sub-directory,
that can be dropped into other CodeIgniter applications.
HMVC stands for Hierarchical Model View Controller.
I don't have experience with hmvc so I cannot tell you what is better. For the standard CI structure, try to separate well in sub-folders (controllers, views and models) related files and try to use helpers to better reuse your code when you need to use functions in more than one place.
I think MyClientBase (which seems to be far from the "standard" exemple), seems to be using HMVC more then MVC.
Related
I built a small module in ASP.net MVC which consists of
a javascript file
a css file
a razor partial view
a c# model class
If I use the standard way to manage the files, I have to spread them in these folders:
/Scripts
/Content/css
/View
/Models
I know that MVC is for separating them and it's fine for the application but i my case I would be happier to have all files somewhere together.
There are area's in MVC but I think that's to big for a small module. All I want is to build a small package in my application for the files of this module.
Any good approach for this? How do you handle this?
/Scripts
/Content/css
/View
/Models
This is just the structure that the default asp.net mvc project template imposes.
You can have any structure you like, but I would recommend keeping your Views and Model separate.
Since our "Model" usually sits in a different assembly our MVC project structure is normally:
Application (all application infastructure code)
Content
css
images
scripts
Controllers
ViewModels
Views
Again this is personal preference but I typically like to keep my static assets (css,images,scripts) under one directory.
You can of course separate things out even further. For example if we're using a javascript plugin that has it's own "core" css and images then we normally keep these together e.g.:
Content
css
images
scripts
libs
myplugin
myplugin.js
myplugin.css
myplugin.png
Of course as you split things out in this way it can become hard to manage. For this reason we use Client Dependency Framework. You could also try Cassette.
I'd just like to get some clarification in the MVC pattern as to what belongs in Models, specifically the contents of the Models folder in MVC3, versus repositories and objects.
Right now, in my current MVC3 Solution, I have 4 projects:
A Project called "Objects", which holds information about all the core objects in my application.
A Project called "Data" which holds information information about the Data Context and repositories for each of the objects (created using MVC3 scaffolding)
The Web project, which holds the Controllers, Views, and -- the subject of this question -- Models
A Unit Testing project
What I really would like to get clarification about is the difference between what should go in the Objects project vs. what goes in the Models folder of the web project. Right now I'm only using the Models folder for holding what I'd call "View Models", which typically contain combinations of the core objects. Should the files in this folder only contain definitions defining the model contents, or should it contain other code that the controller may call?
I think that I have a pretty good understanding of both controllers and repositories, but sometimes I get confused as to whether certain code should go in one or the other. Are there any specific guidelines or limitations out there as to what absolutely should NOT go in a controller but should go in either a repository or a model instead?
Thanks as always.
We do something very similar except that the Objects and Data are combined in a Core library which is referenced by all projects. The models folder in the MVC project is strictly for View Models.
If your controller require additional classes, it really depends on what it needs as to where it goes. I will typically include a Helpers folder with subfolders for HtmlHelpers, Attributes and Filters. If it's a dependency that makes sense to exist outside the MVC project (common classes which are used across all projects) I'll add it to Core.
I'm researching a basic, stand-alone UI app that I want to be 'MVC compliant'.
My question is, what is the typical correlation of the three layers to source code files?
In other words: should I expect to see separate fooView, fooModel and fooController files, or are some functions (eg. Controller) typically specified declaratively and/or handled by a framework?
I realize there are a million MVC frameworks and that the answer probably varies, just looking for a general concept. Cheers and thanks.
Start with the server-side language that you have available and narrow down your MVC framework from there. I would stick with a framework that fits your programming style and needs. Yes, you should have three unique layers (models, views, and controllers) and they should not mix. I.e., in ASP.NET MVC projects you would find a controller, model, and view folder.
I'm building a multi-app site on CodeIgniter. I foresee some of the libraries, helpers, etc. being needed across applications. How do I share such resources between applications? It seems like I can put libraries and helpers under the system folder, but what if I need to share models, controllers, views, too?
This article helped me when developing multiple applications in the same install, I then used .htaccess for different applications and different domain names.
Codeigniter Wiki - Multiple Applications
You would probably have to experiment with the way the folders are set up and the way calls are made in order to access shared models controllers and views.
This post multiple sites, 1 codebase, using symlinks (with smarty) might help but might not be the way you want to do it.
in yii i could have mvc components (acts like an own application). could i have this too in codeigniter?
eg. in SYSTEM/APPLICATION have a folder called COMPONENTS and in there i put stand-alone applications that would be a part of the application. components like ADDRESS BOOK, MAIL, TWITTER and so on. every component folder has folders like: models, views, controllers, config etc.
so a component model extends the application model which in turn extends system's (code igniter) model. the same goes for view and controller.
i've already got a lot of these components which i want to use in codeigniter. is it good idea to place them as i said in SYSTEM/APPLICATION/COMPONENTS or is there best practice for this?
You can do this in CodeIgniter using v1.7.2 or 2.0, but using Packages as Billiam suggested would not work and sadly he is just confusing you.
You are basically looking for a HMVC architecture and this can be provided with a system called Modular Separation.
That works with CodeIgniter 1.7.2 and I have patched it to work with the (still unfinished) CodeIgniter 2.0 branch on the link in the entry.
Not by default in CI 1.7.2, but 'packages' will be available in 2.0.
Added ability to set "Package" paths -
specific paths where the Loader and
Config classes should try to look
first for a requested file. This
allows distribution of
sub-applications with their own
libraries, models, config files, etc.
in a single "package" directory. See
the Loader
class documentation for more
details.
From: http://bitbucket.org/ellislab/codeigniter/src/tip/user_guide/changelog.html
Also, take a look at Modular Extensions - HMVC