tags:

views:

94

answers:

5

I'm thinking of re-working my MVC before I get to far along with it. At the moment it uses a sinle controller which takes the user input and determines the model. The model has maby differ methods which I call actions, because one is called automatically. The model loads its view file, which again holds many different methods. The model can set properties which can be used in the view. Then the controller calls th template classwhich parses the output for display.

Is this the bst way to do it?

Or should each different part (news, contact, about) have its own controller, which loads a specific model and view. Essentially, instead of grouping methods into a single file, it uses multipe files.

I'm kind of lost as to how I should do it.

Cheers

+2  A: 

Start using a MVC that works and is well-known like in Symfony or Cake. From that you will decide:

  • what do to in your own, knowing the best practices;
  • to drop your own if you feel like you can save time by using theses.
e-satis
Good advice.I took a little look at Cake but not much. I'll try to do something with it, see how it works. I want to code my own, I've read a number of articles saying its good to code yiur own , mainly as a learning experience.
Jason Lewis
In normal projects MVC framework is bellow 5% of all code and it is always better to build and adapt MVC to specifications of project. Big open source MVC project are to universal and are slow. But to understand how MVC works and to get some good experience it is good to try some well known MVC FW.
codez
I agree, much of what I've read says that the big MVC projects try to incorporate it all. I plan to make my MVC the bare minimum, so, as you said, I can use it for projects. Adapt it for each project.
Jason Lewis
A: 

Build FrontController which parses request uri and decides which controller to load and which method to run. With .htaccess rewrite all request to index.php

//index.php
class FrontController{
  function run(){
    //parse request uri here /comment/new
    //load controller  comment
    //run controllers method new and pass some request attributes
  }
}

// ../controllers/comment.php
class Controller_Comment extends Controller{
  function new($request){
    //do stuff here
  }
}
codez
+1  A: 

The model loads its view file

The Controller should act as a Mediator between the Model and the View.

The Model shouldn't be aware of the way the view renders it, and also the View shouldn't be aware of any kind of logic of the Model.

In theory, if your MVC is well structured, you should be able to represent the same Model with different types of Views (HTML, XML, JSON for example).

mexique1
A: 

You might want to have a look at the HMVC (Hierarchical MVC) and start implementing.

http://www.javaworld.com/javaworld/jw-07-2000/jw-0721-hmvc_p.html

http://www.thecentric.com/wiki/index.php/HMVC_Tutorial

andreas
+1  A: 

If you are thinking of advancing your own MVC model, like @e-satis have said, you will need to experience what is happening in already developed systems. However, as based on my experience in designing MVC model and determining what is there in opensource community, I stick back to my own MVC for two good reasons. One reason is the flexibility of customization and the other is own MVC privacy.

I used the following approach for MVC design pattern.

A Router.php file identifying user-request urls. This router will be able to fetch controllers and include the file and call the controller default method.

The loaded controller is also able to load other controllers if required to function. This is done using a global method, where all controller Class will extend to a MainController Class which is able to call to other controllers.

I do use a global registry to set and get variables from one controller to the other.

The Models are used to get the Data from Table, and most of my Models will represent Database functions which includes CRUD (Create Read Update Delete). So that a controller can easily manipulate database table data using a model.

Naming conventions in all controller, models, and views is also important, if you want to system to be more intelligent to identify the required action knowing the file name.

I use the Views separately for each type of controller. And these views will be sent to a Master Template View file. Same as models, the controller will be able to set Views to Master View.

There are other customizations which you can do, like applying security methods before calling a class, or after calling a class/controller/model/view etc. This is done by the MainController, which it will always look into a folder with autoload class which states what files should be loaded before and after of different actions during the process of building the content and delivering the output.

MVC is not a small scale idea, but it is a design idea which can always be developed. There are so many PHP MVC open source frameworks to be found if you know how to search the major search engines like google.com

But I do advice you that, MVC is not a good solution if you are simply developing a small dynamic website, as it will consume more time in developing compared to developing small websites. MVC is ideal, if you have business logic and needs system automation in order to avoid most routine tasks of developing, and like that I would say MVC is most ideal for larger applications.

Raf
Thanks, this is really good advice. And I've been re-coding a lot of my MVC over the few days and honestly, it's built a lot like how your system is built. At least, it sounds like it is. I understand the concept of what to use an MVC for, as I already have many plans for it. I've just never really understood how I should be going about it, as most tutorials seem a bit vague to me.
Jason Lewis