开发者

What could I do to improve my MVC?

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 di开发者_如何学Pythonffer 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


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.


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.


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).


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
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜