I'm creating a simple MVC at the moment and am wondering if it's 'correct' to be able to call any model class directly from the controller to get the data to send to the view?

I have the following folder structure:


I also have the needed model classes within the /model folder, at the moment I've tried playing with a /model/model.class.php to try and build all parts of the site but it's already starting to become huge and I think it might be better to completely cut it out.

I'm thinking the better approach is to use:

elseif($_GET['type'] == "Profile") {
    $profile = new thread($userId);
    $comments =  $profile->getComments();
    require "templates/profile.php";

The above is a brief example but is that how a controller could/should/might look or was the first approach better or am I completely missing the point of the model and controller communication?

  • am wondering if it's 'correct' to be able to call any model class directly from the controller Not only correct, but exactly what the controller is supposed to do, as MainMa describes. When you finish with your code, make sure you post it at Code Review SE.
    – yannis
    Commented Jan 7, 2012 at 11:38

4 Answers 4


I'm not sure I completely understand your question, so I apologize if I misinterpreted it.

A controller may have an access to any model of your application. There is no reason to assign a set of models to every controller, and to restrict the access to those models only.

Now, a model is linked to a view (if the view requires a model). If the view HomePage expects the model RecentPosts, the controller has to provide the initialized RecentPosts model to the view.

It's up to the controller to decide where and when to call a view and to pass to it a model this view requires.


In order to allow for separation of concerns your controllers definitely should be able to access multiple models. For example, On a blog site you would need a model to manage your posts and another to manage the users. However when the controller that handles the functionality of retrieving the posts to generate the display view, it likely (depending on how the data has been stored) will need to also access the user model to get names and other such information depending on what the view needs.

As for the rest of your question, I'm not sure if I completely follow your thought process without seeing your code. However I would suggest that you download a few of the available frameworks such as CodeIgniter, CakePHP, FuelPHP, Zend, or others as found here. Take some time and get familiar with different implementations. This will help you in that you'll see what others have tried and what you prefer. I often find it helps to try out other approaches to problems in the process of developing/improving what I'm working on.


Yes, you can allow any controller to call any model. That is actually what MVC is about.

Different frameworks solve that problem in different ways. For example, in Zend Framework, you create new model just by creating new object:

$my_model = new My_Model($someId);

Now it is autoloaders problem to find the physical location of the file this model is in, and require it. You are also able to configure autoloader, in case you want non-standard directory structure.

In Magento, for example, model loading is done via static method:

$my_model = Mage::getModel('my/model', $someId);

In general this does exactly the same thing as above.

As of the directory structure, make sure you don't bloat the entire /model directory. Split them in subdirectories, like this (for My_Model example):

 - my/
 - post/
 - comment/

You can also namespace your models, and make them a little bit more like Javas packages, since this is supported in PHP 5.3 above (if I recall correctly). Zend Framework's autoloader already does this by either simulating namespaces or actaully using them (PHP 5.3 and ZF 2.0 only). To make your life easier, you may use some kind of autoloader. I'd suggest you use the one from Zend Framework, or you can just write one yourself.


The problem with this traditional folder structure is that you have to place e.g. models and views that belong together into different folders. That may create a nightmare. I personally find it hard and time consuming to navigate huge directory structures.

Instead, I found it helpful to follow advice from Derick Bailey's Blog and put all components that belong to one module into corresponding folder. That way you also avoid huge directories but rather have more directories with meaningful names.

Now in your question the word "any" is confusing. If you mean to be able to call any single functions, then certainly no - only those you really need. Otherwise - yes:) (see any source on MVC).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.